在 Python 中使用 Selenium 单击/选择单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21322116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:35:26  来源:igfitidea点击:

Using Selenium in Python to click/select a radio button

pythonseleniumselenium-webdriver

提问by Das Bruno

I am trying to select from a list of 3 buttons, but can't find a way to select them. Below is the HTML I am working with.

我试图从 3 个按钮的列表中进行选择,但找不到选择它们的方法。下面是我正在使用的 HTML。

<input name="pollQuestion" type="radio" value="SRF"> 
    <font face="arial,sans-serif" size="-1">ChoiceOne</font><br />
<input name="pollQuestion" type="radio" value="COM">
    <font face="arial,sans-serif" size="-1">ChoiceTwo</font><br />
<input name="pollQuestion" type="radio" value="MOT">
    <font face="arial,sans-serif" size="-1">ChoiceThree</font>

I can find it by using the following code:

我可以使用以下代码找到它:

for i in browser.find_elements_by_xpath("//*[@type='radio']"):
     print i.get_attribute("value")

This outputs: SRF,COM,MOT

这输出:SRF,COM,MOT

But I would like to select ChoiceOne. (To click it) How do I do this?

但我想选择ChoiceOne。(单击它)我该怎么做?

采纳答案by Yi Zeng

Use CSS Selector or XPath to select by valueattribute directly, then click it.

使用 CSS Selector 或 XPathvalue直接按属性选择,然后单击它。

browser.find_element_by_css_selector("input[type='radio'][value='SRF']").click()
# browser.find_element_by_xpath(".//input[@type='radio' and @value='SRF']").click()

Corrections (but OP should learn how to look up in documentation)

更正(但 OP 应该学习如何在文档中查找)

  • In Python binding, find_elements_by_cssdoesn't exist, it's called find_elements_by_css_selector. One should be able to look at the exception message and look back into documentation hereand figure out why.
  • Notice the difference between find_element_by_css_selectorand find_elements_by_css_selector? The first one finds the first matching element, the second one finds a list, so you need to use [0] to index. Hereis the API documentation. The reason why I use the latter, is because I copied your code, which I shouldn't.
  • 在 Python 绑定中,find_elements_by_css不存在,它被称为find_elements_by_css_selector. 人们应该能够查看异常消息并回顾此处的文档并找出原因。
  • 注意之间的差异find_element_by_css_selectorfind_elements_by_css_selector?第一个找到第一个匹配的元素,第二个找到一个列表,所以需要使用 [0] 来索引。是 API 文档。我之所以使用后者,是因为我复制了您的代码,而我不应该这样做。

回答by Das Bruno

browser.find_elements_by_xpath(".//input[@type='radio' and @value='SRF']")[0].click

This ended up being the fix. I was getting errors without the [0] there, that a list does not have a click() attribute (even though there was only 1 match). Thanks for the help user1177636!

这最终是修复。我在没有 [0] 的情况下遇到错误,即列表没有 click() 属性(即使只有 1 个匹配项)。感谢用户1177636的帮助!

回答by praba230890

find_elements_by_css_selectorworked for me,

find_elements_by_css_selector为我工作,

browser.find_elements_by_css_selector("input[type='radio'][value='SRF']")[0].click()

回答by R P

Enter image description here

在此处输入图片说明

Selenium webdriver Radio button click

Selenium webdriver 单选按钮单击

When i used xpath :

当我使用 xpath 时:

driver.find_element_by_xpath("//input[@id='id_gender2']").click()

radio button not selected

未选中单选按钮

But I used css_selector :

但我使用了 css_selector :

driver.find_element_by_css_selector("input#id_gender1").click() 

radio button selected

已选择单选按钮

回答by kavi

First Radio button was not selected for me also. But after inserting Time it works for me.

也没有为我选择第一个单选按钮。但是在插入时间后它对我有用。

driver.find_element_by_class_name("login").click()
driver.find_element_by_id("email_create").send_keys("[email protected]")
driver.find_element_by_id("SubmitCreate").click()
time.sleep(2)
driver.find_element_by_css_selector("#id_gender2").click()