Selenium Python: How to capture li element with specific text

Question:

I am trying to extract urlToBeCaptured and Text to be captured from the HTML. The structure looks as follows:

<li>
  " text with trailing spaces "
  <a href="urlToBeCaptured">
    <span class ="class1> Text to be captured </span>
    <span class ="class2> Another text </span>
  </a>
...
</li>

I am doing the following, but it doesn’t seem to work:

el = driver.find_element(By.XPATH, "//li[contains(text(),'text with trailing spaces')]") 

Once I locate the element how to extract the text from class1, should it be something like this?

textToBeCaptured = el.find_element(By.CLASS_NAME, 'class1').text
Asked By: bekon

||

Answers:

Given the HTML:

<li>
    text with trailing spaces 
    <a href="urlToBeCaptured">
        <span class ="class1"> Text to be captured </span>
        <span class ="class2"> Another text </span>
    </a>
</li>

To locate the node with text " text with trailing spaces " and then it’s descendent <span> with class ="class1" you can use either of the following locator strategies:

  • Using XPATH and contains():

    print(driver.find_element(By.XPATH, "//li[contains(., 'text with trailing spaces')]/a//span[@class='class1']").text)
    
  • Using XPATH and normalize-space():

    print(driver.find_element(By.XPATH, "//li[contains(normalize-space(),'text with trailing spaces')]/a//span[@class='class1']").text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.by import By
    

Update

From the website to retrive the text 1 person from "Areas of practice at this organisation" with Advocacy you can use the following solution:

  • Code Block:

    driver.get("https://solicitors.lawsociety.org.uk/office/417178/bonallack-bishop")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#ccc-notify-accept > span"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//h2[normalize-space()='Areas of practice at this organisation']"))).click()
    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//section[@class='open']//ul/li[contains(.,'Advocacy')]/a[contains(@href, 'Practice')]/span[@class='accredited-pad']"))).text)
    
  • Console Output:

    1 person
    
  • 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
    
Answered By: undetected Selenium