Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search on YouTube and return all links in Python

On YouTube, I want to search for certain videos (i.e. videos on Python) and after this, I want to return all videos this search returns. Right now if, I try this Python returns all the videos on the start page not on the page after the search.

Current code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://youtube.com")
driver.find_element_by_name("search_query").send_keys("Python")
driver.find_element_by_id("search-icon-legacy").click()
links = driver.find_elements_by_id("video-title")
for x in links:
    print(x.get_attribute("href"))

What goes wrong here?

like image 965
Mark Wekking Avatar asked Jan 01 '23 08:01

Mark Wekking


1 Answers

But is better to use an explicit wait for this:

links = ui.WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "video-title")))

Reference.

Hope it helps you!

like image 194
Ratmir Asanov Avatar answered Jan 15 '23 09:01

Ratmir Asanov