Home > database >  Is there any option to verify/assert element CheckBox is present using Selenium Python
Is there any option to verify/assert element CheckBox is present using Selenium Python

Time:02-17

I'm trying to verify that the checkbox is present but didn't find the exact answer is it possible to check it using Selenium Python (Assert will be great solution).

This is my code:

driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div[1]').click()
time.sleep(1)
driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div[1]/div/div/div[1]/div/ul/li[2]').click()

Now I need to verify whether element with xpath:

/html/body/div[2]/div/div/div[2]/div[1]/div/div/div[1]/div/ul/li[2]

is present ot not?

CodePudding user response:

if you want to check state of object, you can use is_selected() function.

driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div[1]').is_selected()

CodePudding user response:

To verify that the checkbox is present ideally you need to induce WebDriverWait for the presence_of_element_located() and you can use either of the following locator strategies:

  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/div[2]/div[1]/div/div/div[1]/div/ul/li[2]")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Related