To click on Google Search button, as the element is within a <form>
you can use the following Locator Strategies:
Using submit()
:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://google.com")
element = driver.find_element_by_name("q")
element.send_keys('Google')
element.submit()
driver.quit()
print("Test completed")
Using send_keys(Keys.RETURN)
:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://google.com")
element = driver.find_element_by_name("q")
element.send_keys('Google')
element.send_keys(Keys.RETURN)
driver.quit()
print("Test completed")
Using send_keys(Keys.ENTER)
:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://google.com")
element = driver.find_element_by_name("q")
element.send_keys('Google')
element.send_keys(Keys.ENTER)
driver.quit()
print("Test completed")
Ideally, to click on Google Search button, you need to induce WebDriverWait for the element_to_be_clickable()
and you can use the following Locator Strategies:
Using submit()
:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://google.com")
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
element.send_keys('Google')
element.submit()
driver.quit()
print("Test completed")
Using send_keys(Keys.RETURN)
:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://google.com")
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
element.send_keys('Google')
element.send_keys(Keys.RETURN)
driver.quit()
print("Test completed")
Using send_keys(Keys.ENTER)
:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://google.com")
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
element.send_keys('Google')
element.send_keys(Keys.ENTER)
driver.quit()
print("Test completed")
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
References
You can find a couple of relevant detailed discussions in: