1

I have this HTML:

enter image description here

And I want get this text "rataoriginal". (This text changes, I need this part of the code as text)

I've tried

xpath = "//span[@class='_5h6Y_ _3Whw5 selectable-text invisible-space copyable-text']"
auxa = driver.find_element_by_xpath(xpath).text
print(auxa)

But it prints the same as print("\n"). I don't want to use beaultifulsoup for a while.

This HTML is from 'https://web.whatsapp.com'

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

//*[contains(text(),"rataoriginal")] please use this xpath

Justin Lambert
  • 940
  • 1
  • 7
  • 13
0

The WebElement is a dynamic element, so to print the values you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.selectable-text.invisible-space.copyable-text[dir='auto']"))).text)
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, '') and contains(@class, 'invisible-space')][contains(@class, '') and @dir='auto']"))).text)
    
  • 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
    

References

You can find a relevant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352