Home > Software engineering >  How can I find this element with this HTML on Selenium Python?
How can I find this element with this HTML on Selenium Python?

Time:03-18

I'm having a hard time to find a way to select the link below with Selenium by it's name. I need to go by it's name as I will need to identify and select more of it. The identification for this case is: POOL22LATECH

The xpath and CSS selector for this is related to the position on the website and not the name.

Can someone please help?

As an observation, I can't use I can't use the 'tt4', I need to identify by it's name...which, in this case is: POOL22LATECH

Thanks in advance.

HTML:

<a id="tt4" href="javascript:openViewReviewPool('20422', 'Submit for approval', 'fetchPoolAttributes', '1H');">POOL22LATECH<br>20422</a>

XPATH:

//*[@id="tt4"]

CSS Selector:

#poolWithID_3 > td:nth-child(2) > a:nth-child(1)

CodePudding user response:

Have you tried XPath like this ?

"//a[contains(.,'POOL22LATECH')]"

CodePudding user response:

To locate the element with text POOL22LATECH you can use either of the following locator strategies:

  • Using partial_link_text:

    element = driver.find_element(By.PARTIAL_LINK_TEXT, "POOL22LATECH")
    
  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "a[href*='openViewReviewPool'][href*='fetchPoolAttributes']")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//a[contains(@href, 'openViewReviewPool') and contains(., 'POOL22LATECH')]")
    

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

  • Using PARTIAL_LINK_TEXT:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, "POOL22LATECH")))
    
  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href*='openViewReviewPool'][href*='fetchPoolAttributes']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[starts-with(@class, 'contactAction') and starts-with(@aria-label, 'Email')][@href]//span[@class]/span")))
    
  • 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