Home > OS >  Python Selenium I only get the first element from loop
Python Selenium I only get the first element from loop

Time:11-12

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)

CodePudding user response:

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

  • Related