'Selenium - cannot find youtube search bar

Error message :

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: input.ytd-searchbox

I keep getting this error, even though i added a sleep command from other solutions for the page to load dynamically with javascript, but still it cannot find it?

import time
from selenium import webdriver  
firefox = webdriver.Firefox()
firefox.get("https://www.youtube.com")
element = firefox.find_element_by_css_selector("ytd-mini-guide-entry-renderer.style-scope:nth-child(3) > a:nth-child(1)") # opens subscriptions
element.click()
time.sleep(10) # wait for page to load before finding it
searchelement = firefox.find_element_by_css_selector('input.ytd-searchbox') # search bar

searchelement.send_keys("Cute Puppies")
searchelement.submit()


Solution 1:[1]

I just changed the CSS Selector. You did that wrong here. Umm... how i did that? Well there's an easy trick for selecting CSS Selectors.

  1. Type the tag name first. In your case it's input.
  2. If there's an ID present here, type the ID name with # on it. So as i did : #search.
  3. If there's a class there, then use . before it's name. For example .search.

Try this. It's working :

import time
from selenium import webdriver  

firefox = webdriver.Firefox(executable_path=r'C:\Users\intel\Downloads\Setups\geckodriver.exe')
firefox.get("https://www.youtube.com")

element = firefox.find_element_by_css_selector(".style-scope:nth-child(1) > #items > .style-scope:nth-child(3) > #endpoint .title") # opens subscriptions
element.click()

time.sleep(10) # wait for page to load before finding it

searchelement = firefox.find_element_by_css_selector('input#search') # search bar

searchelement.send_keys("Cute Puppies")
searchelement.submit()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1