skip to Main Content

I am trying to click on a checkbox on a website using selenium webdriver. The HTML for the checkbox is this following:

<input type="checkbox" name="registrationForm:certIndCB" id="registrationForm:certIndCB" value="true" onclick="continueAction();">

I tried the following codes:

# first code
driver_wait = WebDriverWait(driver, 10)
cert_info = driver_wait.until(EC.element_to_be_clickable((By.ID, 'registrationForm:certIndCB')))
cert_info.click()
# second code
cert_info = driver.find_element(By.ID,'registrationForm:certIndCB')
cert_info.click()
# third code
cert_info = driver.find_element(By.XPATH, "//input[@id='registrationForm:certIndCB']")
cert_info.click()

But all have returned an error. The first code received a timeout error and the other two send this error:

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
Cell In[6], line 143
    139 time.sleep(3)
    141 # driver_wait = WebDriverWait(driver, 10)
    142 # cert_info = driver_wait.until(EC.element_to_be_clickable((By.ID, 'registrationForm:certIndCB')))
--> 143 cert_info = driver.find_element(By.ID,'registrationForm:certIndCB')
    144 cert_info.click()
    145 wait

I refreshed the page and made sure the elements were not flexing and the id stays as registrationForm:certIndCB. I was under the impression id was the better of the elements to select for a .click(). Does anyone might know why it cannot find the element despite it appearing when I inspect element?

2

Answers


  1. See below code for clicking a web element using JavaScript in python selenium:

    time.sleep(5)
    radioButton = driver.find_element(By.ID,'registrationForm:certIndCB')
    driver.execute_script("arguments[0].click();", radioButton )
    
    
    Login or Signup to reply.
  2. When you use

    (By.ID,'registrationForm:certIndCB')
    

    Selenium converts it to a CSS selector

    #registrationForm:certIndCB
    

    and CSS selectors don’t like ":"s in IDs. I’ve run into this issue before. I found that converting it to a different syntax of CSS selector worked for me.

    [id="registrationForm:certIndCB"]
    

    Below is your original code with a proper wait updated with the new CSS selector.

    driver_wait = WebDriverWait(driver, 10)
    cert_info = driver_wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[id="registrationForm:certIndCB"]')))
    cert_info.click()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search