44

How can I select the checkbox using Selenium with 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).

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

    8 Answers 8

    33

    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 time from selenium import webdriver from selenium.webdriver.common.by import By 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 1 second') time.sleep(1) else: raise e 
    8
    • sorry to tell you that your code is not working in my PC. @falsetru
      – 2964502
      CommentedJan 19, 2014 at 5:24
    • whoops! i am using windows 7 64bit python 3.2, selenium 2.39 @falsetru
      – 2964502
      CommentedJan 19, 2014 at 5:27
    • @viena, I've just test the same code with Python 3.4.0b2, selenium 2.39, and it just worked. BTW, what do you mean it is not working? Does the code cause error? No error, but nothing happended?
      – falsetru
      CommentedJan 19, 2014 at 5:32
    • @viena, What error do you get? selenium.common.exceptions.NoSuchElementException ?
      – falsetru
      CommentedJan 19, 2014 at 5:36
    • yes, with python 3.2 64bit i got raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate element: {"method":"xpath","selector":".//*[contains(text(), \'15 Minute Stream Flow Data: USGS (FIFE)\')]"}' @falstru
      – 2964502
      CommentedJan 19, 2014 at 5:44
    10

    The checkbox HTML is:

    <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:

    Inspect element

    1
    • your code also works given that Waits is used as provided by falsetru
      – 2964502
      CommentedJan 19, 2014 at 6:23
    10

    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() 
    0
      2

      Here's one way to select/click the checkbox with a complete solution.

      this is the website , for example.

      and let's say this is the desired checkbox on the given website (marked under red loop) to perform a click.

      solution:

      import time from selenium.webdriver import Chrome from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait import selenium.webdriver.support.expected_conditions as EC driver = Chrome() driver.get('https://register.whirlpool.com/en-us/registration') WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'privacy_policy'))) driver.execute_script("document.getElementById('privacy_policy').click();") time.sleep(2) var1 = driver.find_element(By.ID, "privacy_policy").is_selected() print(var1) 

      output:

      True 
      1. First, we wait and locate the element By the available methods(here By.ID) to make sure that the target checkbox element is loaded/located on the page.
      2. Next, execute the javascript query (here document.getElementById('privacy_policy').click()) to click on the checkbox.
      3. We can also verify if the click or the select was performed on the desired checkbox using the method is_selected() as mentioned above.
      4. It'll return True if the checkbox was clicked/selected and False otherwise.

      You can also cross-check by simply running the javascript query document.getElementById('privacy_policy').click() on the Console of the page and you'll see that it indeed performs the click on the desired checkbox.

        1

        This is the best approach to click by using 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 expression here") browser.execute_script("arguments[0].click();", box) 
        1
        • I agree, I also use this approach.
          – vitaliis
          CommentedJun 21, 2023 at 6:01
        0

        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() 
          0

          The best way to handle the element "Checkbox" if it has text is to use a JavaScript function to extract it from the webpage:

          For example: //*[text()='text on element']

          Here also you can use the same while extracting the checkbox and use the click() function to check it.

          driver = webdriver.Chrome() url = "URl of site" driver.get(url) checkbox = driver.find_element_by_xpath(//*[text()='text on element']) checkbox.click() 

          To check whether an element got checked or not, you may use the get_attribute() function.

          For example: checkbox.get_attribute('checked')

            0

            I recommend reading this

            https://splinter.readthedocs.io/en/latest/api/driver-and-element-api.html?highlight=radio#splinter.driver.DriverAPI.choose

            browser.check("checkbox_name") 

              Start asking to get answers

              Find the answer to your question by asking.

              Ask question

              Explore related questions

              See similar questions with these tags.