pkk
pkk

Reputation: 379

unable fetch date from youtube scrape selenium

Here is the screenshot of text i want to fetch:

enter image description here

full link to inspect element : -

https://www.youtube.com/watch?v=KwyVTQaBndU

I want to fetch the Jun 21, 2021 from the tag please attached screenshot.

Here is the code I tried:

try:
    postdate = driver.find_element_by_xpath("//*[@id='container']//div[@id='info']//div[@id='date']").get_attribute(
        'innerText')
    print(postdate)
except Exception as e:
    print(e)

output:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='container']//div[@id='info']//div[@id='date']"}
  (Session info: chrome=91.0.4472.106)

it was working till date but i guess youtube has done some changes in format. So it is not working.

Upvotes: 1

Views: 186

Answers (2)

cruisepandey
cruisepandey

Reputation: 29362

A reliable css selector would be :

span#dot+yt-formatted-string

and there's a typo I think, it should be innerHTML

use it like this :

try:
    postdate = driver.find_element_by_css_selector("span#dot+yt-formatted-string").get_attribute('innerHTML')
    print(postdate)
except Exception as e:
    print(e)

Upvotes: 2

CatChMeIfUCan
CatChMeIfUCan

Reputation: 569

you can use one of the following ways

X-path (which currently you are using)

postdate = driver.find_element_by_xpath('//*[@id="info-strings"]/yt-formatted-string').text
print(postdate)

Full X-Path

postdate = driver.find_element_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[6]/div[2]/ytd-video-primary-info-renderer/div/div/div[1]/div[2]/yt-formatted-string').text
print(postdate)

CSS-Selector

postdate = driver.find_element_by_css_selector('#info-strings > yt-formatted-string').text
print(postdate)

there is also another way

postdate = driver.find_element_by_xpath('//*[local-name()="yt-formatted-string" and @class="style-scope ytd-video-primary-info-renderer"]/parent::div/parent::div/parent::div')
print(postdate)

try these ways it should work!

Upvotes: 0

Related Questions