Select checkbox using Selenium with Python

后端 未结 6 1855
鱼传尺愫
鱼传尺愫 2020-12-25 10:45

How can I select the checkbox using Selenium with Python?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.         


        
相关标签:
6条回答
  • 2020-12-25 11:17

    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

    0 讨论(0)
  • 2020-12-25 11:19

    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 讨论(0)
  • 2020-12-25 11:22

    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 讨论(0)
  • 2020-12-25 11:27

    Use find_element_by_xpath 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.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
    
    0 讨论(0)
  • 2020-12-25 11:39

    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)
    
    0 讨论(0)
  • 2020-12-25 11:41

    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 讨论(0)
提交回复
热议问题