我目前有一个脚本,它在已经存在的下拉菜单中选择一个选项。我需要脚本来刷新页面,直到出现一个下拉菜单,其中包含我需要选择的选项。
答案 0 :(得分:2)
一旦选择了所需的选项,您就可以进行无限循环:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.get("url")
wait = WebDriverWait(driver, 10)
DESIRED_OPTION = "My desired option"
while True:
dropdown_element = wait.until(EC.visibility_of_element_located((By.ID, "#dropdownID")))
dropdown = Select(dropdown_element)
selected_option = dropdown.first_selected_option.text
print("Selected option is '%s'" % selected_option)
if selected_option == DESIRED_OPTION:
break
driver.refresh()
此代码假定您的下拉列表是通过select
- > option
表示的。