Python selenium wait until not waiting to search 出现

Python selenium wait until not waiting to search appear

我试图等到搜索应用于 optgroup,但 WebDriverWait.until 方法似乎什么都不做。 这是它在 chrome inspect 上的样子:


<select name="potentialrecipients[]" id="potentialrecipients" multiple="multiple" size="20" class="form-control no-overflow">
  <optgroup label="Potential badge recipients (1)">
    <option value="39676">Daniel (12345, daniel@blabla.com)</option>
  </optgroup>
</select>

这是我的代码:


searchBox = driver.find_element_by_id('potentialrecipients_searchtext')
searchBox.send_keys('12345')

element = WebDriverWait(driver, 10).until(driver.find_element_by_xpath("//select[@id='potentialrecipients']/optgroup[@label='Potential badge recipients (1)']/option"))

element.click()

这是我遇到的错误:


elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@id='potentialrecipients']/optgroup[@label='Potential badge recipients (1)']/option"}
  (Session info: chrome=80.0.3987.149)

当我尝试使用调试等待搜索应用时,它工作得很好。

我做错了什么? 谢谢!!

引发 WebDriverWait() 和 element_to_be_clickable() 并跟随 xpath

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//select[@id='potentialrecipients']//option[@value='39676']"))).click()

您需要导入以下库。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC