在 Python 中使用 Selenium 选择复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21213417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select check box using Selenium with Python
提问by 2964502
Would be nice if someone knows how to select the checkbox using Selenium with Python.
如果有人知道如何使用 Selenium 和 Python 来选择复选框,那就太好了。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
url = 'any url'
browser.get(url)
browser.find_element_by_id("15 Minute Stream Flow Data: USGS (FIFE)").click()
I want to select the checkbox corresponding to 15 Minute Stream Flow Data: USGS (FIFE.
我想选中与 15 分钟河流流量数据对应的复选框:USGS (FIFE.
I tried as id, name, link_textbut could not detect what should be used?
我试过id, name,link_text但无法检测到应该使用什么?
采纳答案by falsetru
Use find_element_by_xpathwith the xpath expression .//*[contains(text(), 'txt')]to find a element that contains txtas text.
find_element_by_xpath与 xpath 表达式.//*[contains(text(), 'txt')]一起使用以查找包含txt文本的元素。
browser.find_element_by_xpath(
".//*[contains(text(), '15 Minute Stream Flow Data: USGS (FIFE)')]"
).click()
UPDATE
更新
Some contents are loaded after document load. I modified the code to try 10 times (1s sleep inbetween).
某些内容在文档加载后加载。我修改了代码以尝试 10 次(中间有 1 秒的睡眠时间)。
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Firefox()
url = 'http://reverb.echo.nasa.gov/reverb/'
browser.get(url)
for i in range(10):
try:
browser.find_element_by_xpath(
".//*[contains(text(), '15 Minute Stream Flow Data: USGS (FIFE)')]"
).click()
break
except NoSuchElementException as e:
print('retry in 1s.')
time.sleep(1)
else:
raise e
回答by Maria Ines Parnisari
The checkbox HTML is:
复选框 HTML 是:
<input id="C179003030-ORNL_DAAC-box" name="catalog_item_ids[]" type="checkbox" value="C179003030-ORNL_DAAC">
so you can use
所以你可以使用
browser.find_element_by_id("C179003030-ORNL_DAAC-box").click()
One way you can find elements' attributes is using the Google Chrome Developer Tools:
查找元素属性的一种方法是使用 Google Chrome 开发人员工具:


回答by Carlo 1585
You can try in this way as well:
你也可以这样试试:
browser.find_element_by_xpath(".//*[@id='C179003030-ORNL_DAAC-box']")
If you want know if it's already checked or not:
如果你想知道它是否已经被检查过:
browser.find_element_by_xpath(".//*[@id='C179003030-ORNL_DAAC-box']").get_attribute('checked')
to click:
点击:
browser.find_element_by_xpath(".//*[@id='C179003030-ORNL_DAAC-box']").click()
回答by Carlo 1585
so you can try this as well:
所以你也可以试试这个:
browser = webdriver.Firefox()
url = 'http://reverb.echo.nasa.gov/reverb/'
browser.get(url)
browser.find_element_by_name("catalog_item_ids[]").click()
回答by Monic Annadurai
This is the best way of approach to click by using selenium python
这是使用 selenium python 单击的最佳方法
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
url = 'any url'
browser.get(url)
box=browser.find_element_by_xpath("paste the xpath here")
browser.execute_script("arguments[0].click();",box)

