1

I'm trying to print out the title on youtube videos and when I print it out in my loop it only prints out the first element and I don't know why.

Here's my python code

s = Service('C:\webdrivers\chromedriver.exe')

driver = webdriver.Chrome(service=s)
driver.get("https://www.youtube.com/c/TechWithTim/videos?view=0&sort=p&flow=grid")

videos = driver.find_elements(By.CLASS_NAME, 'style-scope ytd-grid-renderer')
titles = []
for video in videos:
    title = video.find_element(By.XPATH, './/*[@id="video-title"]')
    print(title.text)
QJ123123
  • 93
  • 1
  • 7
  • If you aren't locked into using selenium for this it would be better (easier, more stable, less code) to use the Youtube API for this (https://stackoverflow.com/a/20795628/1581658) – SamBob Nov 12 '21 at 08:51
  • 1
    I just wanted to try to learn selenium, but thanks anyways. – QJ123123 Nov 12 '21 at 08:54

1 Answers1

0

you dont select the right elements:

videos = driver.find_elements(By.XPath, "//div[@id='items' and @class='style-scope ytd-grid-renderer']//a[@id='video-title']")
titles = []
for video in videos:
    title = video.text
    titles.append(title)
    print(title)

//div[@id='items' and @class='style-scope ytd-grid-renderer']//a[@id='video-title']

means

search all tags a which have an id = video-title, and have a parent div which has an id= items and class = style-scope ytd-grid-renderer

Frenchy
  • 16,386
  • 3
  • 16
  • 39