我如何使用 expected_conditions.text_to_be_present_in_element_value,但 _text 参数是元组中的任何字符串?

How can I use expected_conditions.text_to_be_present_in_element_value, but with the _text parameter being any String from a tuple?

我使用下面的代码等待一分钟,或者直到 EC.text_to_be_present_in_element_value_text 参数等于字符串 to_inp:

# Python
to_inp = "secret"
WebDriverWait(driver, 60).until(EC.text_to_be_present_in_element_value((By.ID, "user_guess"), to_inp))

<!-- HTML -->
<input type="text" class="form-control text-center" id="user_guess">

效果很好,但为了实际使用,我需要它来检查字符串是否在元组中,例如:

# Python
to_inp = ('ananas', 'banana', 'mango') # text in input has to be equal to any of those
WebDriverWait(driver, 60).until(EC.text_to_be_present_in_element_value((By.ID, "user_guess"), to_inp))

<!-- HTML -->
<input type="text" class="form-control text-center" id="user_guess">

我收到以下错误:

TypeError: 'in <string>' requires string as left operand, not tuple

selenium中有presence_of_all_elements_located你能想到的

to_inp = ('ananas', 'banana', 'mango')
new_list = WebDriverWait(driver, 60).until(EC.presence_of_all_elements_located((By.ID, "user_guess")))
sorted(to_inp, key=lambda x: x[1])
new_list.sort()
b = list(set(to_inp).intersection(set(new_list)))
print(b)

您可以根据 expected_conditions

中的 text_to_be_present_in_element_value 编写自己的实现
class TextToBePresentInElementValue:

    def __init__(self, locator, texts):
        self.locator = locator
        self.texts = texts

    def __call__(self, driver):
        try:
            element_text = driver.find_element(*self.locator).get_attribute("value")
            return element_text in self.texts
        except StaleElementReferenceException:
            return False

to_inp = ('ananas', 'banana', 'mango')
WebDriverWait(driver, 60).until(TextToBePresentInElementValue((By.ID, "user_guess"), to_inp))