Select checkbox using Selenium with Python

2024/11/18 5:45:33

How can I select the checkbox using Selenium with Python?

from selenium import webdriver
from selenium.webdriver.common.keys import Keysbrowser = 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).

I tried as id, name, link_text, but I could not detect it. What should be used?

Answer

Use find_element with the XPath expression .//*[contains(text(), 'txt')] to find a element that contains txt as text.

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 (1 second sleep in between).

import timefrom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementExceptionbrowser = 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()breakexcept NoSuchElementException as e:print('Retry in 1 second')time.sleep(1)
else:raise e
https://en.xdnf.cn/q/26529.html

Related Q&A

linear programming in python?

I need to make a linear programming model. Here are the inequalities Im using (for example):6x + 4y <= 24 x + 2y <= 6 -x + y <= 1 y <= 2I need to find the area described by these inequaliti…

Beginner Python Practice? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pylint: Disable specific warnings for specific folder

We have a Python project laid out like this:project/ ├── .pylintrc ├── module1.py ├── module2.py └── tests/├── test_module1.py└── test_module2.pyOur unit and function tests reside in …

PyCharm: Configuring multi-hop remote Interpreters via SSH

To connect to the computer at my office I need to run ssh twice. First to connect to the host-1 and then from host-1 to host-2 and each one has different credentials. However the configuration menu in …

Return results from multiple models with Django REST Framework

I have three models — articles, authors and tweets. Im ultimately needing to use Django REST Framework to construct a feed that aggregates all the objects using the Article and Tweet models into one r…

Comparing XML in a unit test in Python

I have an object that can build itself from an XML string, and write itself out to an XML string. Id like to write a unit test to test round tripping through XML, but Im having trouble comparing the tw…

Passing a tuple as command line argument

My requirement is to pass a tuple as command line argument like --data (1,2,3,4)I tried to use the argparse module, but if I pass like this it is receiving as the string (1,2,3,4). I tried by giving ty…

Setting exit code in Python when an exception is raised

$ cat e.py raise Exception $ python e.py Traceback (most recent call last):File "e.py", line 1, in <module>raise Exception Exception $ echo $? 1I would like to change this exit code fr…

cmake error the source does not appear to contain CMakeLists.txt

Im installing opencv in ubuntu 16.04. After installing the necessary prerequisites I used the following command:-kvs@Hunter:~/opencv_contrib$ mkdir build kvs@Hunter:~/opencv_contrib$ cd build kvs@Hunte…

Overlay an image segmentation with numpy and matplotlib

I am trying to overlay two images. The first one is a 512x512 NumPy array (from a CT image). The second one is also a 512x512 NumPy array but I am just interested in the pixels where the value is large…