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?
To return all videos from the search with the keyword as Python you need to:
You can use the following solution
Code Block:
Console Output:
But is better to use an explicit wait for this:
Reference.
Hope it helps you!
As per the discussion with @Mark:
It seems that the elements of the first page of Youtube are still in the DOM...
The only fix I see is to go to the search URL:
You should use WebDriverWait not sleep:
The output: