我正在尝试使用以下代码来使用selenium(python)从网页的下拉列表中选择一个选项。
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
path_to_chromedriver = 'C:/Users/User_1/chromedriver/chromedriver'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'https://lifeinsurance.rac.com.au/rac/get-a-quote?productid=51'
browser.get(url)
wait = WebDriverWait(browser, 10)
# residence listbox
drop_down = browser.find_element_by_xpath('//*[@id="divApplicantDetails__1"]/div[8]/div/div[2]/span/span/span[1]')
browser.execute_script('arguments[0].style.display="inline";', drop_down)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="divApplicantDetails__1"]/div[8]/div/div[2]/span/span/span[1]')))
wait.until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="ddResidentialStatusId__1_listbox"]/li[1]'))).click()
有时它可以工作(并且选择了下拉列表中的选项),但在其他情况下,未选择下拉列表中的选项(也不会引发错误)。
对于如何从提供的代码中提到的下拉列表中始终如一地选择一个选项,我将不胜感激。
答案 0 :(得分:1)
def select_resident_status(resident_status):
// click the arrow down to make all options list out
driver.find_element_by_css_selector('span[aria-owns*="ResidentialStatus"] span.k-select').click()
// select option by pass-in option text
driver.find_element_by_css_selector('div[id*="ResidentialStatus"][style*="display: block"] ul')
.find_element_by_xpath('./li[text()="'+resident_status+'"]').click()
答案 1 :(得分:1)
我不会尝试操纵元素的CSS。只需像用户那样接近它...点击下拉列表,点击一个选项。我使用了不同的定位器,但这对我有用。
driver.find_element_by_css_selector("span[aria-owns='ddResidentialStatusId__1_listbox']")).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#ddResidentialStatusId__1_listbox > li")))
options = driver.find_elements_by_css_selector("#ddResidentialStatusId__1_listbox > li"))
options[0].click() # clicks "Australian citizen or permanent resident"
python没有像Java这样的visibilityOfAllElementsLocatedBy()
,所以我不得不等待一个元素,然后获取所有元素,然后单击第一个元素。如果你真的只想点击第一个,你可以做到
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ddResidentialStatusId__1_listbox > li"))).click()
并跳过最后两行代码。我把它保留原样是因为我假设你会想要(绝地精神力量在你身上工作)把它扔进一个函数并选择不同的选项。
答案 2 :(得分:1)
First you can click on the select box which is always clickable
driver.find_element_by_xpath("//span[@class='k-input' and text()='Select residential status']").click()
Then you can click on the desired item only when the list of residential status is visible (aria-hidden='false')
driver.find_element_by_xpath("//ul[@id='ddResidentialStatusId__1_listbox' and @aria-hidden='false']/li[@class='k-item' and text()='Other']").click()
In order to work, this solution need to setup implict wait for your script
driver = webdriver.Chrome(executable_path = path_to_chromedriver)
driver.implicitly_wait(5)
Edit: I do more tests and this solution doesn't works at each time. So I suggest you to add a small pause even it's preferable to avoid this. In this case I think it's not breakable as it's only used in order to allow the list box to be rendered completely.
driver.find_element_by_xpath("//span[@class='k-input' and text()='Select residential status']").click()
driver.find_element_by_xpath("//ul[@id='ddResidentialStatusId__1_listbox' and @aria-hidden='false']")
time.sleep(1)
driver.find_element_by_xpath("//ul[@id='ddResidentialStatusId__1_listbox' and @aria-hidden='false']/li[@class='k-item' and text()='Other']").click()
答案 3 :(得分:0)
我记得在没有 Select() 的情况下,我在 Selenium 的下拉菜单中遇到了一些非常烦人的问题
要使用 ActionsChains,您需要导入以下内容...
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
<块引用>
通过文本选择下拉选项要容易得多
actions = ActionsChains(browser)
dropdown = browser.find_element(By.XPATH, "//*[text()='dropdown option']").click()
actions.click(on_element=dropdown)
actions.perform()
如果这不起作用,请尝试插入 time.sleep。