简体   繁体   English

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

[英]Using Selenium in Python to click/select a radio button

I am trying to select from a list of 3 buttons, but can't find a way to select them.我正在尝试从 3 个按钮的列表中转到 select,但找不到转到 select 的方法。 Below is the HTML I am working with.下面是我正在使用的 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.但我想select ChoiceOne。 (To click it) How do I do this? (单击它)我该怎么做?

Use CSS Selector or XPath to select by value attribute directly, then click it.使用 CSS Selector 或 XPath 直接按value属性选择,然后单击它。

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_css doesn't exist, it's called find_elements_by_css_selector .在 Python 绑定中, find_elements_by_css不存在,它被称为find_elements_by_css_selector One should be able to look at the exception message and look back into documentation here and figure out why.人们应该能够查看异常消息并回顾此处的文档并找出原因。
  • Notice the difference between find_element_by_css_selector and find_elements_by_css_selector ?注意到find_element_by_css_selectorfind_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.第一个找到第一个匹配的元素,第二个找到一个列表,所以需要使用 [0] 来索引。 Here is the API documentation. 是 API 文档。 The reason why I use the latter, is because I copied your code, which I shouldn't.我之所以使用后者,是因为我复制了您的代码,而我不应该这样做。

在此处输入图片说明

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已选择单选按钮

find_elements_by_css_selector为我工作,

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

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("testsel000@gmail.com")
driver.find_element_by_id("SubmitCreate").click()
time.sleep(2)
driver.find_element_by_css_selector("#id_gender2").click()

Consider that you have a radio button to select either of the two options, "Male or "Female". Then try using the following:- This is in Python (Selenium).考虑到您有一个单选按钮 select 两个选项之一,“男性”或“女性”。然后尝试使用以下内容:- 这是在 Python (Selenium) 中。

driver.find_element_by_xpath("//label[contains(text(),'Male')]").click() driver.find_element_by_xpath("//label[contains(text(),'Male')]").click()

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).我在没有 [0] 的情况下遇到错误,即列表没有 click() 属性(即使只有 1 个匹配项)。 Thanks for the help user1177636!感谢用户1177636的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM