1

I want to extract the first span with the text Extract this text. Already tried:

element.find_element_by_css_selector(".moreContent span:nth-child(1)").text.strip('"')

This is not working, I am not sure why. The output is just empty.

<p class="mainText">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry.
  <span class="moreEllipses">…&nbsp;</span>
  <span class="moreContent">
    <span> Extract this text </span>
    <span class="link moreLink">Show More</span>
  </span>
</p>

However I am getting this, so Selenium finds the element but why the output is empty:

<selenium.webdriver.remote.webelement.WebElement (session="e7012b303842651848aa0b0e40f5d5c1", element="df5644e9-fc98-4300-ad86-9ff433154d82")>

EDIT:

I managed to solve this by clicking on show more button. For some reason i can't extract the content if not visible even if present in page.

anvd
  • 3,997
  • 19
  • 65
  • 126

2 Answers2

0

As per your cssSelector it seems you are targeting below

<span> Extract this text </span>

You can use below Xpath:

(//p[@class='mainText']//span[@class='moreContent']/span)[1]

OR

(//span[@class='moreContent']/span)[1]

Example Code:

element = driver.find_element_by_xpath("(//p[@class='mainText']//span[@class='moreContent']/span)[1]").text
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
0

To extract the text from the first <span> i.e. Extract this text you need to to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and text property:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p.mainText span.moreContent>span"))).text)
    
  • Using XPATH and get_attribute() method:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[@class='mainText']//span[@class='moreContent']/span"))).get_attribute("innerHTML"))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352