[英]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 应该学习如何在文档中查找)
find_elements_by_css
doesn't exist, it's called find_elements_by_css_selector
.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.find_element_by_css_selector
and find_elements_by_css_selector
?find_element_by_css_selector
和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.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.