Selenium 'send_keys' does not work for YouTube

Issue

I’m new to Selenium and I’m trying to create my first automatization test where OS is gonna open Chrome browser, opens YouTube and enteres a word in the search bar.
Well, browser opens, YouTube opens, but OS doesn’t enter any word.
This is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//*[@id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')

Solution

Seems like there could be 2 element in the page with the same id as search, we need to get the input element having id search.

The below code, with the changes should work fine now.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[@id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')

Answered By – Himanshu Poddar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published