且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:1970-01-01 07:56:42

使用CSS选择器或XPath直接通过value属性进行选择,然后单击它.

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

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

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

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

  • 在Python绑定中,find_elements_by_css不存在,称为find_elements_by_css_selector.一个人应该能够查看异常消息并回顾文档此处是API文档.之所以使用后者,是因为我复制了您的代码,但我不应该这样做.
  • In Python binding, find_elements_by_css doesn'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 here and figure out why.
  • Notice the difference between find_element_by_css_selector and 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. Here is the API documentation. The reason why I use the latter, is because I copied your code, which I shouldn't.