Google Search Automation with Python Selenium

SeleniumAutomation TestingTesting Tools

We can perform Google search automation with Selenium webdriver in Python. First of all, we shall locate the Google search box with the help of any of the locators like id, css, xpath, class, or name.

Then simulate the action of pressing the ENTER key with the help of Keys.ENTER/Keys.RETURN. To perform this operation, we have to use the method send_keys and then pass the parameter – Keys.RETURN /Keys.ENTER. Also, we have to add the statement - from selenium.webdriver.common.keys import Keys to use the Keys class.

Example

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.google.com/")
#identify search box
m = driver.find_element_by_name("q")
#enter search text
m.send_keys("Tutorialspoint")
time.sleep(0.2)
#perform Google search with Keys.ENTER
m.send_keys(Keys.ENTER)

Output

raja
Published on 06-Apr-2021 11:55:04
Advertisements