Selenium find_element_by_id() doesn't work
Question:
When I go to https://www.youtube.com with Selenium and try to select the search bar using find_element_by_id() and using "masthead-search-term" as the ID, I get an error like this:
Traceback (most recent call last):
File "C:UserslauriDesktopProjectsPython Projectstest.py", line 9, in <module>
driver.find_element_by_id("masthead-search-term")
File "D:Pythonlibsite-packagesseleniumwebdriverremotewebdriver.py", line 341, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "D:Pythonlibsite-packagesseleniumwebdriverremotewebdriver.py", line 843, in find_element
'value': value})['value']
File "D:Pythonlibsite-packagesseleniumwebdriverremotewebdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "D:Pythonlibsite-packagesseleniumwebdriverremoteerrorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"masthead-search-term"}
Why is that? Here’s the code:
import selenium.webdriver as webdriver
driver = webdriver.Chrome(executable_path="D:Applicationschromedriver")
driver.get("https://www.youtube.com")
driver.find_element_by_id("masthead-search-term")
Answers:
Your code is correct. Use some wait to locate the element. Try this code:
import selenium.webdriver as webdriver
driver = webdriver.Chrome(executable_path="D:Applicationschromedriver")
driver.get("https://www.youtube.com")
driver.implicitly_wait(10)
driver.find_element_by_id("masthead-search-term").send_keys("Nature")
This is Java code (set geckodriver if required):
WebDriver driver = new FirefoxDriver();
driver.get("https://www.youtube.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("masthead-search-term")).sendKeys("Nature");
You can also use the name as a locator:
driver.findElement(By.name("search_query")).sendKeys("Nature");
As the exception states, there isn’t any element on the page with id="masthead-search-term"
.
Make sure you are looking for the correct id by inspecting the page DOM. Look for the id attribute of the search bar input element.
I think the search bar input element id is search
.
I think you are trying to find the search bar and send keys and search. There doesn’t exist an element with ID ‘masthead-search-term’.
I would suggest a better way to do this is:
search_query = your_search_query
driver.get('https://www.youtube.com/results?search_query=' + search_query)
If you want to do it in the manner you have done, then it will look something like:
driver.get(http://www.youtube.com)
driver.find_element_by_id('search').sendKeys('Your_search_query')
driver.find_element_by_id('search-icon-legacy').click()
One of the more interesting ways is to use Action Chains:
driver.get(http://www.youtube.com)
actions = ActionChains(driver)
actions.send_keys('Your_Search_Query')
actions.send_keys(Keys.TAB)
actions.send_keys(Keys.ENTER)
actions.perform()
It is a browser issue. When I use Chrome, Firefox or Edge I cannot find the element. When I use Internet Explorer it is found.
When you use the IEDriver it probably will work but with the ChromeDriver it will not.
You have to take care of a couple of things as follows:
-
While working with the Chrome browser, try to open in maximized mode, with disabling infobars and extensions which enables you to use the maximum available Viewport
.
-
Once you access the URL https://www.youtube.com, either wait for the loader to disappear or wait for the search box to be clickable.
-
Ensure the locators you are using are unique and correct. With Dev Tools of the Firefox Quantum 57.0b3 (64-bit) browser, the XPath expression of the search box seems to be //input[@id='search']
.
-
Here is the working code block with browser:
import selenium.webdriver as webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
driver.get("https://www.youtube.com")
search_box = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//input[@id='search']"))
)
driver.find_element_by_xpath("//input[@id='search']").send_keys("Selenium")
When I go to https://www.youtube.com with Selenium and try to select the search bar using find_element_by_id() and using "masthead-search-term" as the ID, I get an error like this:
Traceback (most recent call last):
File "C:UserslauriDesktopProjectsPython Projectstest.py", line 9, in <module>
driver.find_element_by_id("masthead-search-term")
File "D:Pythonlibsite-packagesseleniumwebdriverremotewebdriver.py", line 341, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "D:Pythonlibsite-packagesseleniumwebdriverremotewebdriver.py", line 843, in find_element
'value': value})['value']
File "D:Pythonlibsite-packagesseleniumwebdriverremotewebdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "D:Pythonlibsite-packagesseleniumwebdriverremoteerrorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"masthead-search-term"}
Why is that? Here’s the code:
import selenium.webdriver as webdriver
driver = webdriver.Chrome(executable_path="D:Applicationschromedriver")
driver.get("https://www.youtube.com")
driver.find_element_by_id("masthead-search-term")
Your code is correct. Use some wait to locate the element. Try this code:
import selenium.webdriver as webdriver
driver = webdriver.Chrome(executable_path="D:Applicationschromedriver")
driver.get("https://www.youtube.com")
driver.implicitly_wait(10)
driver.find_element_by_id("masthead-search-term").send_keys("Nature")
This is Java code (set geckodriver if required):
WebDriver driver = new FirefoxDriver();
driver.get("https://www.youtube.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("masthead-search-term")).sendKeys("Nature");
You can also use the name as a locator:
driver.findElement(By.name("search_query")).sendKeys("Nature");
As the exception states, there isn’t any element on the page with id="masthead-search-term"
.
Make sure you are looking for the correct id by inspecting the page DOM. Look for the id attribute of the search bar input element.
I think the search bar input element id is search
.
I think you are trying to find the search bar and send keys and search. There doesn’t exist an element with ID ‘masthead-search-term’.
I would suggest a better way to do this is:
search_query = your_search_query
driver.get('https://www.youtube.com/results?search_query=' + search_query)
If you want to do it in the manner you have done, then it will look something like:
driver.get(http://www.youtube.com)
driver.find_element_by_id('search').sendKeys('Your_search_query')
driver.find_element_by_id('search-icon-legacy').click()
One of the more interesting ways is to use Action Chains:
driver.get(http://www.youtube.com)
actions = ActionChains(driver)
actions.send_keys('Your_Search_Query')
actions.send_keys(Keys.TAB)
actions.send_keys(Keys.ENTER)
actions.perform()
It is a browser issue. When I use Chrome, Firefox or Edge I cannot find the element. When I use Internet Explorer it is found.
When you use the IEDriver it probably will work but with the ChromeDriver it will not.
You have to take care of a couple of things as follows:
-
While working with the Chrome browser, try to open in maximized mode, with disabling infobars and extensions which enables you to use the maximum available
Viewport
. -
Once you access the URL https://www.youtube.com, either wait for the loader to disappear or wait for the search box to be clickable.
-
Ensure the locators you are using are unique and correct. With Dev Tools of the Firefox Quantum 57.0b3 (64-bit) browser, the XPath expression of the search box seems to be
//input[@id='search']
. -
Here is the working code block with browser:
import selenium.webdriver as webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By options = Options() options.add_argument("start-maximized") options.add_argument("disable-infobars") options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe') driver.get("https://www.youtube.com") search_box = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, "//input[@id='search']")) ) driver.find_element_by_xpath("//input[@id='search']").send_keys("Selenium")