Home > OS >  Find element by the value attribute using Selenium Python
Find element by the value attribute using Selenium Python

Time:04-01

I`m trying to find a specific element in the web page. It has to be by a specific number inside the text in value...In this case, I need to find the element by the number 565657 (see outer HTML below)

I've tried via xpath: ("//*[contains(text(),'565657')]") and also ("//input[@value='565657']") but it did not work.

Can someone please help? Thanks in advance!

The original XPATH when you copy from the element on the web page is just ' //*[@id="checkbox15"]' and I need to find a way to look for the number I mentioned below.

CodePudding user response:

To locate the element through the value attribute i.e. 565657 you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "input[value*='565657']")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//input[contains(@vlaue, '565657')]")
    

To locate ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[value*='565657']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@vlaue, '565657')]")))
    
  • 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