EDIT: Youtube looks different in mobile Firefox than on Desktop Chrome. Your question is somehow confusing, are you looking for the instant search results, or are you looking for actual results of the search? I edited the code below to include both scenarios.
The following code will open the page, dismiss the cookie button (if appears), correctly locate and click on the search button, send some keys into the searchbox, locate and printout the instant search results, send Enter to perform the search, and go through results (scroll the page) until it reaches the end of the page It is down to the OP to implement a break from that While loop (it's not hard).
It should all be self explanatory (it also uses Firefox), setup is for Linux, but you just have to observe the imports, and the code after defining the browser.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
firefox_options = Firefox_Options()
firefox_options.add_argument("--width=1280")
firefox_options.add_argument("--height=720")
firefox_options.set_preference("general.useragent.override", "Mozilla/5.0 (Linux; Android 7.0; SM-A310F Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36 OPR/42.7.2246.114996")
driverService = Service('chromedriver/geckodriver')
browser = webdriver.Firefox(service=driverService, options=firefox_options)
url = 'https://www.youtube.com/'
browser.get(url)
try:
cookie_button = WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.XPATH,'//button[@aria-label="Reject the use of cookies and other data for the purposes described"]')))
print(cookie_button.location_once_scrolled_into_view)
t.sleep(1)
cookie_button.click()
print('rejected cookies')
except Exception as e:
print('no cookie button')
t.sleep(1)
search_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//div[@class="mobile-topbar-header-content non-search-mode cbox"]/button[@aria-label="Search YouTube"]/c3-icon')))
search_button.click()
t.sleep(1)
search_field = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//input[@name="search"]')))
search_field.send_keys('programming is hard')
t.sleep(1)
results = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,'//ul[@role="listbox"]/li[@role="presentation"]')))
print('here are the instant search results:')
for r in results:
print(r.text)
t.sleep(1)
print('and now we perform the search, by sending Enter key')
search_field.send_keys(Keys.ENTER)
while True:
results_after_pressing_enter = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,'//a[@class="compact-media-item-metadata-content"]')))
print('and here are the results of the search:')
for res in results_after_pressing_enter:
print(res.text)
print('___________________')
browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")
t.sleep(2)
This will print in terminal:
{'x': 486, 'y': 405}
rejected cookies
here are the instant search results:
programming is hard
programming is hard and i feel unmotivated
programming is hard to learn
programming is too hard
programming is not hard
game programming is hard
programming hardware
programming hardware with python
programming hard music
programming is easy or hard
and now we perform the search, by sending Enter key
and here are the results of the search:
Why is coding so hard...
TechLead
253K views3 years ago
___________________
coding is hard...
Joma Tech
717K views3 years ago
___________________
Why Is Programming Difficult?
Andy Sterkowitz
101K views3 years ago
[...]