1

I would like to create a code snippet for opening YouTube, accepting the cookies, finding the search bar, typing some string into it and finally clicking on the search button. Its not too hard but something is not working. I have tried using the WebDriverWait as well but still not working.

If I open the Google (of course from code) and doing the same procedure then everything works well. I have tried finding elements not only XPATH but also ID, and CSS_SELECTOR.

After the send_keys() function the error message is:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

My code is:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_experimental_option('detach', True)
options.add_experimental_option('excludeSwitches', ['enable-logging'])

driver = webdriver.Chrome(options=options)
driver.get("https://www.youtube.com/")
driver.maximize_window()

cookie_accept = driver.find_element(By.XPATH, '//*[@id="content"]/div[2]/div[6]/div[1]/ytd-button-renderer[2]/yt-button-shape/button/yt-touch-feedback-shape/div/div[2]')
cookie_accept.click()

yt_searchbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="search"]')))
yt_searchbox.send_keys('Python Selenium')

I've tried without the WebDriverWait as well:

yt_search = driver.find_element(By.XPATH, '//*[@id="search"]')
yt_search.send_keys("Pyhton Selenium")

And it is not working either. I don't know what's going on.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • Maybe [YouTube Data API v3](https://developers.google.com/youtube/v3) [Search: list](https://developers.google.com/youtube/v3/docs/search/list) endpoint will interest you. – Benjamin Loison Feb 19 '23 at 19:52

1 Answers1

0

The Search box within Youtube homepage is a dynamic element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.youtube.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys("Python Selenium")
    
  • Using XPATH:

    driver.get('https://www.youtube.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='search']"))).send_keys("Python Selenium")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser snapshot:

youtubeSerarch

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have tried not only using xpath but also css_selector and I am still getting the same error: Message: stale element reference: element is not attached to the page document – JuhaszPeter1337 Feb 20 '23 at 18:10
  • By the way if I use time.sleep(1) between the cookie and the send_keys() section, its working properly, so this EC.element_to_be_clickable() is not working at all. – JuhaszPeter1337 Feb 20 '23 at 22:07
  • @JuhaszPeter1337 Oh, I don't face a cookie from APAC region :) Maybe cookie enforced for Euro region – undetected Selenium Feb 20 '23 at 22:09