Selenium webdriver python, cannot find by value?

柔情痞子 提交于 2020-05-27 13:02:13

问题


I'm trying to select one of three checkboxes on a page but the source code for them looks like this:

<input type="radio" name="Option" value="1">
<input type="radio" name="Option" value="2">
<input type="radio" name="Option" value="3">

So the only thing differentiating them is value, but there is no find_element_by_value. How would I go about switching between these 3 checkboxes?


回答1:


You can use xpath for identification of elements in such cases,

browser.find_element_by_xpath("//input[@value='1']")

For a better understanding of how xpaths work, you can refer the below link http://www.toolsqa.com/selenium-webdriver/choosing-effective-xpath/




回答2:


I will use cssSelector to identify this. There is no mechanism to find the element by value. Use find_element_by_css_selector with the following selector. Refer the api

input[value='3']

or, to be more precise

input[value='3'][type='radio']

Changing the value would definitely let you grab what you need.

Also, possible to use xpath with find_element_by_xpath for this

//input[@value='2']


来源:https://stackoverflow.com/questions/28610407/selenium-webdriver-python-cannot-find-by-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!