Home > Mobile >  Selenium 'send_keys' does not work for YouTube
Selenium 'send_keys' does not work for YouTube

Time:07-01

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')

CodePudding user response:

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')
  • Related