使用 selenium (python) 从条件下拉列表中选择一个选项 [英] Using selenium (python) to select an option from a conditional dropdown

查看:56
本文介绍了使用 selenium (python) 从条件下拉列表中选择一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

这很好用.我需要填充的下一个项目也是一个选择/选项下拉列表.可用选项由第一个选择的选项查询.在 firebug 中,看起来他们正在调用函数来查询第二个下拉列表中的结果.所以接下来我尝试只单击选择元素 id,它应该调用函数来查询选项.然后我让它等待(尝试了长达 10 秒),然后选择该选项.

#点击select元素调用函数driver.find_element_by_xpath("//select[@id='_fid_74']").click()# 等待查询功能完成driver.implicitly_wait(2)# 选择选项driver.find_element_by_xpath("//select[@id='_fid_74']/option[@value='142']").click()

这给了我两个错误之一:

在缓存中未找到元素 - 可能页面自查找后已更改

文件C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py",第 107 行,在 check_response 中消息 = 值["值"]["消息"]类型错误:字符串索引必须是整数

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

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

大家有什么建议吗?

解决方案

首先,您应该使用 Select 使得处理 select 和它的选项非常简单并且抽象了复杂性:

from selenium.webdriver.support.select import 选择select = Select(driver.find_element_by_xpath("//select[@id='_fid_74']"))select.select_by_value("142")

<小时><块引用>

字符串索引必须是整数

这是一个已知问题在硒 2.49 中.目前,您需要降级到 2.48:

pip install selenium==2.48

I'm trying to automate some admin tasks at work, so I'm using selenium to manipulate our project management platform (quickbase).

The first item I populate is a select/options drop down, which I can select a record by using the xpath selector:

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

This works just fine. The next item I need to populate is also a select/options drop down. The available options are queried by the first selected option. In firebug it looks like they are calling the function to query the results on click of the 2nd drop down. So next I tried just clicking the select element id, which should call the function to query the options. Then I let it wait (tried for as long as 10 seconds), and select the option.

# 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()

This gives me one of two errors:

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

or

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

I also tried returning a list of elements, looping through them and checking if the value '142' and if so click that element object. That gave me an error as well. I've tried using xpath: //select[@id='_fid_74']/option[text()='the text'] which didn't work either.

I assume it has something to do with the conditional selection, since the same query worked for the first dropdown (first code block).

Anyone have any suggestions?

解决方案

First of all, you should use the Select class that makes dealing with select and it's options quite simple and abstracts away the complexity:

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

This is a known issue in selenium 2.49. Currently, you need to downgrade to 2.48:

pip install selenium==2.48 

这篇关于使用 selenium (python) 从条件下拉列表中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆