Inputting data in Google Search using Selenium
Google Search

Inputting data in Google Search using Selenium

Hello all, in this article, we will focus on the use of Selenium in searching for any word.

Let us begin:

  1. 1st of all we will import our necessary libraries. Again "Selenium" is the name of the testing library with which we perform tests, "webdriver" is the program that helps selenium get the web application, "keys" is the library module that will help us to hit enter key thereby reducing extra line of code and decreasing complexity and "time" here, to put delay so that we can notice automation before window gets closed.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time        

2. Now loading the chromedriver.exe so as to automate chrome. Remember: Different browser has their unique driver.

driver = webdriver.Chrome("chromedriver.exe")        

3. Assigning our target link to the driver

driver.get("https:\\www.google.co.in")        

4. Now extracting the XPath of the input field in google:

No alt text provided for this image

And assign it to a variable in which the element of the input field will be located

box = driver.find_element_by_xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input")        

We can see the content in the double inverted quotes. Now to make it easy to understand we can modify it. here everything is a part of HTML form so "/html/body -> //" and since the number of "div" in the code can be changed in the future (It can never be the same) so we put "*" asterisk in between 2 HTML elements. In this case, it's "between body and form" so any number of div will come in asterisk as the "form" will not change. Therefore, the modified code is:

box = driver.find_element_by_xpath("https://*/form/div[1]/div[1]/div[1]/div/div[2]/input")        

5. We can see the input element by printing it:

print("The input Element is: ", box)        

6. Now we send our "Search Text" to the input field. "send_keys" function is used to send search text:

box.send_keys("Python")        

7. Now in place of extracting the element of the Search button (That will ofcourse extend the time and space complexity) we simply write code to hit on entering button in the input field)

box.send_keys(Keys.RETURN)        

8. Finally we put a sleep of 5 seconds to see our search result to arrive and close the driver.

time.sleep(5)
driver.close()        

Our Complete Code Looks like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome("chromedriver.exe")
driver.get("https:\\www.google.co.in")
box = driver.find_element_by_xpath("https://*/form/div[1]/div[1]/div[1]/div/div[2]/input")
print("The input Element is: ", box)
box.send_keys("Send")
box.send_keys(Keys.RETURN)
time.sleep(5)
driver.close()        

Output:

No alt text provided for this image

We can see the Input element here:

No alt text provided for this image

Hope you all find the Content useful :)

要查看或添加评论,请登录

Kartikey Garg的更多文章

  • Crypto Price Prediction Report

    Crypto Price Prediction Report

    Disclaimer This project was developed with the assistance of an AI companion, Microsoft Copilot. The AI generated all…

  • The Growing Generative AI

    The Growing Generative AI

    Warm Greetings to all, let's proceed further without taking any further time in welcoming because as you read this…

  • Encourage employees for Wide Knowledge

    Encourage employees for Wide Knowledge

    Hello, working professionals, I hope you all are having a great day at your job. My name is Kartikey Garg, a fresher…

  • Web login using Selenium

    Web login using Selenium

    In this article, we will study the use of Selenium. We will practice login into Facebook.

  • Magic 8 Ball Game

    Magic 8 Ball Game

    Today we are code for the "Magic 8 ball" game in Python Our Assumptions: A list of answers representing 8 balls The…

  • Python Number Guessing

    Python Number Guessing

    In this article, we are going to make another basic python project on "Number guessing" Our assumptions: A total of 3…

    2 条评论
  • The Dice Rolling Simulator

    The Dice Rolling Simulator

    In this article, we are going to form the small and simple project of "Rolling Dice in Python". I will be explaining…

  • Reversing Time: When Time goes Negative

    Reversing Time: When Time goes Negative

    Hello Everyone, hope all are doing well and great, I was eager to share my own thoughts regarding time travel. Today's…

社区洞察

其他会员也浏览了