Text Searching in Google using Selenium in Python
Last Updated :
26 Oct, 2022
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python.
In this article, we are going to see how to automate our browser. We can just select the word/sentence and speak Search and the word/sentence gets automatically searched and provide you with accurate results.
Requirement:
- pyautogui: PyAutoGUI is a cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
- selenium : Selenium is a powerful tool for controlling web browsers through programs and performing browser automation.
- speech_recognition: Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc.
- We are using chromedriver_autoinstaller so that then we could see the meaning of the searched word. [Have Installed Chrome Latest Version on your local device).
Step-by-step Approach:
Step 1: Import required modules
Python3
# import module.
# Web browser Automation
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Support for chrome
import chromedriver_autoinstaller
# Invoking speech module
import speech_recognition as sr
# Support file for speech recognition
import pyttsx3
# Automating task
import pyautogui
Step 2: Let's invoke the speech recognition module and initiate our internal speaker so that it could hear our voice as input and could initiate the process. MyText stores our voice command as text.
Python
r = sr.Recognizer()
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration = 0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = str(MyText.lower())
Step 3: After selecting and speaking search using your voice will now initiate the process. Using selenium and pyautogui automatically takes that word and gives the appropriate search result.
Python3
if MyText == "search":
# Automates 'copy' internally
pyautogui.hotkey('ctrl', 'c')
chrome_options = webdriver.ChromeOptions()
capabilities = {'browserName': 'chrome',
'javascriptEnabled': True}
capabilities.update(chrome_options.to_capabilities())
chromedriver_autoinstaller.install()
# Invoking the chrome
driver = webdriver.Chrome()
# Adjusting the size of the window
driver.set_window_size(1920, 1080)
driver.implicitly_wait(10)
driver.get("https://www.google.com/")
#Place where our selected word gets pasted
driver.find_element(By.XPATH,
"/html/body//form[@role='search']/div[2]/div[1]//div[@class='a4bIc']/input[@role='combobox']")
.send_keys(pyautogui.hotkey('ctrl', 'v'))
Below is the full implementation:
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import chromedriver_autoinstaller
import speech_recognition as sr
import pyttsx3
import pyautogui
while(True):
try:
r = sr.Recognizer()
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration = 0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = str(MyText.lower())
if MyText == "search":
pyautogui.hotkey('ctrl', 'c')
chrome_options = webdriver.ChromeOptions()
capabilities = {'browserName': 'chrome', 'javascriptEnabled': True}
capabilities.update(chrome_options.to_capabilities())
chromedriver_autoinstaller.install()
driver = webdriver.Chrome()
driver.set_window_size(1920, 1080)
driver.implicitly_wait(10)
driver.get("https://www.google.com/")
driver.find_element(By.XPATH,
"/html/body//form[@role='search']/div[2]/div[1]//div[@class='a4bIc']/input[@role='combobox']")
.send_keys(pyautogui.hotkey('ctrl', 'v'))
elif MyText == "stop":
break
except Exception as e:
pyautogui.press('enter')
Demo:
Similar Reads
Search Google Using Python Selenium Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. This art
1 min read
Get all text of the page using Selenium in Python As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A
3 min read
How to scrape multiple pages using Selenium in Python? As we know, selenium is a web-based automation tool that helps us to automate browsers. Selenium is an Open-Source testing tool which means we can easily download it from the internet and use it. With the help of Selenium, we can also scrap the data from the webpages. Here, In this article, we are g
4 min read
Click button by text using Python and Selenium Selenium is a tool that provides APIs to automate a web application to aid in its testing. In this article, we discuss the use of Selenium Python API bindings to access the Selenium WebDrivers to click a button by text present in the button. In the following example, we take the help of Chrome. The
2 min read
Python Selenium - Find element by text The technique to verify if the requirements given by the user meet with the actual software product developed is known as Software Testing. Moreover, it also checks if the final software product developed is error-free or not. Software testing can either be performed manually or with the help of sof
3 min read