当我使用手动检查选中一个下拉框时,总是会提供一个值。 当我使用selenium来选择值时,有时会提供一些选项
select = Select(self.driver.find_element_by_xpath(xpath))
print(",".join( [o.text for o in select.options])
选项有时是空的
就像用户单击下拉框时可以填充选项一样。 我尝试了以下所有方法
wait = WebDriverWait (brower, 10)
optionValue = xpath + "/option[.='Value']"
wait.until(EC.presence_of_element_located((By.XPATH, optionValue)))
...
select.select_by_visible_text("Value")
...
select.select_by_index(1)
select没有打开菜单的click()方法。你知道吗
你有什么建议?你知道吗
自我回答——深深的歉意
实际上,下拉菜单是在一个弹出窗口中,由一个按钮触发。当弹出窗口触发得太早时,它就永远不会被填满。因此,我添加了一个触发弹出窗口的测试,检查下拉列表的内容,如果下拉列表不包含值,我退出,等待一段时间,然后再做一次
nbAttemps = 5
waitTime = 2
optionIsFound = False
while nbAttemps > 0:
nbAttemps = nbAttemps - 1
self.driver.find_element_by_xpath("//button[.='No distribution']").click()
selectXpath="//div/div/bla bla bla"
select = Select(self.driver.find_element_by_xpath(selectXpath))
options = [o.text for o in select.options]
optionIsFound = "Value" in options
logger.debug("Options in select are " + ",".join( options))
logger.debug("nbAttemps = %d" % nbAttemps )
logger.debug("optionIsFound= %s, Value not in %s" % (optionIsFound,options))
if not optionIsFound:
# close the popup
webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
time.sleep(waitTime)
else:
select.select_by_visible_text("Value")
nbAttemps = 0
您可以首先使用
click()
方法单击下拉框,然后从下拉框中选择值。你知道吗如果下拉列表显示悬停在下拉列表上的列表,那么可以使用Action类,然后使用
moveToElement(element)
悬停在下拉列表上,然后使用click()
方法单击下拉列表中所需的值。你知道吗尝试选项
让我知道它是否有效。你知道吗
相关问题 更多 >
编程相关推荐