简体   繁体   English

如何使用selenium选择单选按钮

[英]How to select radio button using selenium

I am trying to click this radio button using selenium and python. 我试图使用selenium和python单击此单选按钮。

<input type="radio" name="tweet_target_1" value="website" class="tweet-website-button radio-selection-validate serialize-me newline-before field-order-15">

I have 我有

website = driver.find_element(name="tweet_target_1")
website.click()

but it's not allowing me to click it. 但它不允许我点击它。 How can I click using a combo of name, value or class, value etc.? 如何使用名称,值或类,值等组合单击?

Is there a good source of info about how to use selenium? 关于如何使用硒有很好的信息来源吗? Because most of what I've found is on java and I'm using python. 因为我发现的大部分内容都是在java上,而我正在使用python。

EDIT: using XPATH 编辑:使用XPATH

I tried 我试过了

website = driver.find_elements(By.XPATH, "//form[@id='dmca_form' and @class='twitter-form custom-form']/div[20][@class='list-container']/div[1][@class='list-item']/div[7][@class='clearfix inf-tweet init-hide']/div[@class='input']/ul[@class='options']/li[2]/label/input[@class='tweet-website-button radio-selection-validate serialize-me newline-before field-order-15']/")
website.click()

I keep getting 我一直在

AttributeError: 'list' object has no attribute 'click'

I know this comes a little too late perhaps, but I joined just recently. 我知道这可能有点太迟了,但我刚刚加入。

Tip: Use, Firebug and with it Firepath. 提示:使用,Firebug和Firepath。 Locate the radio button and find out the xpath for the element in question. 找到单选按钮,找出相关元素的xpath。

website = driver.find_element_by_xpath(".//**")
website.click()

OR 要么

website = driver.find_element_by_xpath(".//**").click()

This should work all the time you try. 这应该在您尝试时始终有效。 Also, just using from selenium import webdriver should make the click() function work correctly. 另外,只需使用from selenium import webdriver可以使click()函数正常工作。

I'm not sure where you found the documentation that said you could call find_element like that, but you should either be doing driver.find_element_by_name("tweet_target_1") or driver.find_element(By.NAME, "tweet_target_1") (having first imported By of course). 我不确定你在哪里找到说可以像这样调用find_element的文档,但是你应该做的是driver.find_element_by_name("tweet_target_1")或者driver.find_element(By.NAME, "tweet_target_1") (先导入By课程)。 Also, Selenium Java code is pretty easily convertible to Python code; 此外,Selenium Java代码很容易转换为Python代码; it follows a few pretty simple transformation rules, and if you still have questions, all the code for the library itself will also be on your machine to look at. 它遵循一些非常简单的转换规则,如果您仍有疑问,库本身的所有代码也将在您的机器上查看。

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

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