I'm trying to extract the description from a youtube channel:
driver.get(channel_url+"/about")
try:
myElem = WebDriverWait(driver, 3).until(lambda d: d.find_element("xpath","//*[@id=\"description\"]"))
print(myElem.text)
except TimeoutException: print("failed")
This is what the HTML code looks like:
<yt-formatted-string id="description" split-lines="" class="style-scope ytd-channel-about-metadata-renderer">
Text I want to extract
</yt-formatted-string>
I am using a gecko Firefox driver
The .text
and get_attribute("textContent")
commands both return nothing. The get_attribute("innerHTML")
command returns an irrelevent <path></path>
element.
I copied the xpath from my browser using Inspect.
Youtube has 3-4 elements having id
"description"
(I dont know why.")
You can find it if you search with ctrl+f in developers tools or using the method find_all()
So try using full xpath as such:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
channel_url = "https://www.youtube.com/@MrBeast"
driver = webdriver.Chrome()
driver.get(channel_url+"/about")
try:
myElem = WebDriverWait(driver, 3).until(lambda d: d.find_element("xpath",'/html/body/ytd-app/div[1]/ytd-page-manager/ytd-browse/ytd-two-column-browse-results-renderer/div[1]/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-channel-about-metadata-renderer/div[1]/div[1]/yt-formatted-string[2]'))
print(myElem.text)
except TimeoutException: print("failed")
driver.quit()
Or use the proper CSS selector pointing only to the single element:
(Obviously, you can try same way with xpath too)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
channel_url = "https://www.youtube.com/@MrBeast"
driver = webdriver.Chrome()
driver.get(channel_url+"/about")
try:
myElem = WebDriverWait(driver, 3).until(lambda d: d.find_element(By.CSS_SELECTOR,'#description-container > #description'))
print(myElem.text)
except TimeoutException: print("failed")
driver.quit()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments