gpt4 book ai didi

python - 使用 selenium (python) 从条件下拉列表中选择一个选项

转载 作者:行者123 更新时间:2023-12-01 04:10:03 25 4
gpt4 key购买 nike

我正在尝试自动化工作中的一些管理任务,因此我使用 selenium 来操作我们的项目管理平台(quickbase)。

我填充的第一项是选择/选项下拉列表,我可以使用 xpath 选择器选择记录:

driver.find_element_by_xpath("//select[@id='_fid_67']/option[@value='28']").click()

这工作得很好。我需要填充的下一个项目也是一个选择/选项下拉列表。通过第一个选定的选项查询可用选项。在 Firebug 中,他们似乎正在调用该函数来查询单击第二个下拉菜单时的结果。接下来我尝试单击选择元素 id,这应该调用函数来查询选项。然后我让它等待(尝试了长达 10 秒),然后选择该选项。

# click the select element to call function
driver.find_element_by_xpath("//select[@id='_fid_74']").click()

# wait for the query function to finish
driver.implicitly_wait(2)

# select the option
driver.find_element_by_xpath("//select[@id='_fid_74']/option[@value='142']").click()

这给了我两个错误之一:

Element not found in the cache - perhaps the page has changed since it was looked up

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 107, in check_response
message = value["value"]["message"]
TypeError: string indices must be integers

我还尝试返回元素列表,循环遍历它们并检查值是否为“142”,如果是,则单击该元素对象。这也给了我一个错误。我尝试过使用 xpath: //select[@id='_fid_74']/option[text()='文本']这也不起作用。

我认为它与条件选择有关,因为相同的查询适用于第一个下拉列表(第一个代码块)。

大家有什么建议吗?

最佳答案

首先,您应该使用 Select class这使得处理 select 及其选项变得非常简单,并消除了复杂性:

from selenium.webdriver.support.select import Select

select = Select(driver.find_element_by_xpath("//select[@id='_fid_74']"))
select.select_by_value("142")
<小时/>

string indices must be integers

这是一个known issue Selenium 2.49。目前,您需要降级到2.48:

pip install selenium==2.48 

关于python - 使用 selenium (python) 从条件下拉列表中选择一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com