如何使用 Python 使用 Selenium 选择下拉菜单值?
我需要从下拉菜单中选择一个元素。
例如:
<select id="fruits01" class="select" name="fruits">
<option value="0">Choose your fruits:</option>
<option value="1">Banana</option>
<option value="2">Mango</option>
</select>
1) 首先我必须单击它。我这样做:
inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()
2) 之后我必须选择好的元素,比如说Mango
。
我尝试使用 inputElementFruits.send_keys(...)
来完成此操作,但它不起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
Selenium 提供了一个方便的
Select
类 与select -> 一起使用option
构造:另请参阅:
Selenium provides a convenient
Select
class to work withselect -> option
constructs:See also:
除非您的点击触发某种 ajax 调用来填充您的列表,否则您实际上不需要执行点击。
只需找到该元素,然后枚举选项,选择所需的选项即可。
这是一个示例:
您可以在以下位置阅读更多内容:
https://sqa.stackexchange.com/questions/ 1355/无法使用seleniums-python-webdriver选择选项
Unless your click is firing some kind of ajax call to populate your list, you don't actually need to execute the click.
Just find the element and then enumerate the options, selecting the option(s) you want.
Here is an example:
You can read more in:
https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver
我希望这段代码能对您有所帮助。
带有 id 的下拉元素
带有 xpath 的下拉元素
带有 css 选择器的下拉元素
从下拉列表中选择“Banana”
ddelement.select_by_index(1)
ddelement.select_by_value('1')
ddelement.select_by_visible_text('香蕉')
I hope this code will help you.
dropdown element with id
dropdown element with xpath
dropdown element with css selector
Selecting 'Banana' from a dropdown
ddelement.select_by_index(1)
ddelement.select_by_value('1')
ddelement.select_by_visible_text('Banana')
首先,您需要导入 Select 类,然后需要创建 Select 类的实例。
创建 Select 类的实例后,您可以在该实例上执行 select 方法以从下拉列表中选择选项。
这是代码
firstly you need to import the Select class and then you need to create the instance of Select class.
After creating the instance of Select class, you can perform select methods on that instance to select the options from dropdown list.
Here is the code
根据提供的 HTML:
从 html-select 菜单,您必须使用 选择 类。此外,由于您必须与 下拉菜单你必须诱导WebDriverWait
element_to_be_clickable()
。从 dropdown 您可以使用以下任一定位器策略:
使用ID 属性和
select_by_visible_text()
方法:使用CSS-SELECTOR和
select_by_value()
方法:使用XPATH和
select_by_index()
方法:< /p>As per the HTML provided:
To select an
<option>
element from a html-select menu you have to use the Select Class. Moreover, as you have to interact with the drop-down-menu you have to induce WebDriverWait for theelement_to_be_clickable()
.To select the
<option>
with text as Mango from the dropdown you can use you can use either of the following Locator Strategies:Using ID attribute and
select_by_visible_text()
method:Using CSS-SELECTOR and
select_by_value()
method:Using XPATH and
select_by_index()
method:我尝试了很多事情,但我的下拉菜单位于表格内,我无法执行简单的选择操作。只有下面的解决方案有效。在这里,我突出显示下拉 elem 并按向下箭头,直到获得所需的值 -
I tried a lot many things, but my drop down was inside a table and I was not able to perform a simple select operation. Only the below solution worked. Here I am highlighting drop down elem and pressing down arrow until getting the desired value -
您无需单击任何内容。
使用 xpath 查找或您选择的任何内容,然后使用发送键
对于您的示例:
HTML:
Python:
就是这样。
You don't have to click anything.
Use find by xpath or whatever you choose and then use send keys
For your example:
HTML:
Python:
That's it.
您可以使用 css 选择器组合,
将 css 选择器 attribute = value 中的 1 更改为与所需水果对应的值。
You can use a css selector combination a well
Change the 1 in the attribute = value css selector to the value corresponding with the desired fruit.
通过这种方式,您可以选择任何下拉列表中的所有选项。
In this way you can select all the options in any dropdowns.
在浏览了很多像这样的帖子之后,我设法找到了一种解决方案,允许我在下拉列表中选择一个项目。我以各种方式尝试了 .send_keys、click() 和 Select,但没有成功。在单击下拉列表中的项目之前,最终向下拉列表发送了 3 次 click() 命令。
绝对不是超级漂亮,但它确实有效。
希望这对某人有帮助。这是在 Firefox 88.0.1 上使用 Python3.7.7 完成的。
After going through a lot of posts like this one, I managed to figure out a solution that allowed me to select an item in a dropdown. I tried .send_keys, click(), and Select in various ways with no success. Ended up sending the click() command to the dropdown 3 times before clicking on the item in the dropdown.
Definitely not super pretty, but it works.
Hope this helps someone. This was done with Python3.7.7 on Firefox 88.0.1.
使用以下方式您可以选择下拉值。
Using Following Way You can Select the dropdown value.
它适用于选项值:
It works with option value:
我将其用于所有点击和选择,并且它始终有效。对于下拉项,只需确保 xpath 是您要选择的实际值。
I use this for all of my clicks and selecting and it always works. For a dropdown item just make sure the xpath is the actual value you want to select.
它会工作得很好
It will work fine
没有
每次我面对没有
导入
ActionChains module
使用
ActionChains
点击元素Dropdown WITHOUT
<select>
This works for me every time I face a dropdown without
<select>
tagsImport
ActionChains
moduleUse
ActionChains
to click on the element它对我的情况有效的唯一方法是:
所有其他解决方案都不起作用,因为该值总是立即重置。
The only way it worked for my case was:
All the other solutions didnt work as the value always resetted immediately.
使用
selenium.webdriver.support.ui.Select
类进行下拉选择的最佳方法,但有时由于设计问题或 HTML 的其他问题,它无法按预期工作。在这种情况下,您还可以使用
execute_script()
作为替代解决方案,如下所示:-The best way to use
selenium.webdriver.support.ui.Select
class to work to with dropdown selection but some time it does not work as expected due to designing issue or other issues of the HTML.In this type of situation you can also prefer as alternate solution using
execute_script()
as below :-