Search code examples
pythonselenium-webdriverweb-scrapingyoutube

Youtube Videos Title scraping - Selenium


Don't know why I am getting error while fetching the video title element on Youtube. I tried every possible way. Please give a solution.

I tried this code :

list = []
video_title = driver.find_elements(By.CSS_SELECTOR, "yt-formatted-string")
for i in video_title:
    text_title = i.text
    list.extend(text_title)
print(list)

and I am getting this error:

File "C:\Users\Admin\PycharmProjects\scraping\Bot\Youtube\Y_data.py", line 25, in <module>
    text_title = i.text
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

List of videos title


Solution

  • First you have to go through the video

    videos = driver.find_elements(by=By.CLASS_NAME, value='style-scope ytd-grid-video-renderer')
    

    then you can scrap the title using

    title = video.find_element(by=By.XPATH, value='.//*[@id="video-title"]').text
    

    value of XPATH and CLASS_NAME may be different in some case