0

My case is simple, I just want to take the text from this html bellow:

<yt-formatted-string id="text" title="" class="style-scope ytd-channel-name">Rayra Fortunato</yt-formatted-string>

I'm using Python + Selenium but i can't retrieve the "Rayra Fortunato" from it as a string.

This is my code that isn't working properly.

realName = driver.find_element_by_xpath("//yt-formatted-string[contains(text(),'Rayra Fortunato')]")
realName = realName.get_attribute('text')
print(realName)

or

realName = driver.find_element_by_xpath("//yt-formatted-string[contains(text(),'Rayra Fortunato')]")
realName = realName.text
print(realName)

Any help?

Thanks!

2
  • Rayra Fortunato is expected output?' Commented Jul 8, 2021 at 1:47
  • Is there any error ? Commented Jul 8, 2021 at 6:38

2 Answers 2

1

There are many possible variations you can try. Among them:

locator = driver.find_element_by_xpath('//yt-formatted-string[@id="text"]')
real_name = locator.text

Or

locator = driver.find_element_by_css_selector("text")
real_name = locator.text

Or:

locator = driver.find_element_by_xpath('//yt-formatted-string[@id="text"]')
real_name = locator.get_attribute("innerHTML")

(and the same method with css_selector).

However, this should work for this small html. The real HTML that you are looking at may be much more complex and above solutions might not work. If it's true check the following:

  • Locator is not located inside iframe
  • Locator is not located inside a shadow DOM
  • Page is fully loaded before you are trying to get the text
  • Locators are unique (it's hard to say from your question).

This list may be longer.

Sign up to request clarification or add additional context in comments.

Comments

1

You may need explicit wait as well, however it seems that your locator is correct.

Code :

wait = WebDriverWait(driver, 10)
print(wait.until(EC.element_to_be_clickable((By.XPATH, "//yt-formatted-string[contains(text(),'Rayra Fortunato')]"))).text)

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.