Google Search Automation with Python Selenium

0 votes

Hi,

I want to know how to automate google search in python selenium.

I am done with below steps;

  1. Open chrome
  2. Open Google.com
  3. Enter value by sendkeys

I am stuck at below;

  1. When I put search item 'Software Testing' it given suggested search then I want to know 
  2. How to select any result by down arrow key
  3. I could click the 'Google Search' but if I want to do multiple tabs and then need to reach on 'Google Search' button, how to do that ?

I tried with 

WebElement.sendKeys(Keys.RETURN); but it wont work and there is issue with 

  1. sendKeys, send_Keys, and send.Keys syntax
  2. Issue with TAB and Tab. RETURN and Return. 

Please suggest.

Thanks in Advance

Adi.

Aug 5, 2019 in Selenium by ADS
• 280 points
11,634 views
search = "something"

driver.get("https://www.google.com/search?q="+search)

1 answer to this question.

0 votes

You can simply automate pressing the "Enter" key instead of finding and clicking the search button. Here's the script:

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

driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"))
driver.get("http://www.google.com")

que=driver.find_element_by_xpath("//input[@name='q']")
que.send_keys("Software testing")
que.send_keys(Keys.RETURN)
answered Aug 5, 2019 by Payal
Thanks Payal,

But its not working. I tried. It is giving an error with 'RETURN' as undefined variable from import.
Where are you running the Python script? The code I mentioned worked fine on PyCharm.
Eclipse Latest Version !

Version: 2019-06 (4.12.0)
Build id: 20190614-1200

Try ENTER:

que.send_keys(Keys.ENTER)
No, Its not working.

There is one work around where we need to tick the undefined items as ignore via

Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Undefined -> Undefined Variable From Import -> Ignore in Eclispe. I tried that too but its not working.
Hi,

It worked for

que.submit()

but it was working only for the value first put ie "Software Testing". If we want to click on other searches like software testing life cycle, software testing jobs etc then how to do that. ?

You can use the down arrow key to select the autosuggestion options. This works:

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

driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"))
driver.get("http://www.google.com")

que=driver.find_element_by_xpath("//input[@name='q']")
que.send_keys("Software testing")
time.sleep(2)
que.send_keys(Keys.ARROW_DOWN)
que.send_keys(Keys.ARROW_DOWN)
time.sleep(2)
que.send_keys(Keys.RETURN)
Thanks ! That Worked for me.
Thank you payal, for providing the solution for this issue.
@Payal, thanks for the code. its working fine for me.
Try This 
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path="C:\Program Files\chromedriver")
driver.set_page_load_timeout("10")
driver.get("https://www.google.com/")
driver.maximize_window()
que = driver.find_element_by_name("q")
que.send_keys("Software Testing")
time.sleep(3)
que.send_keys(Keys.ARROW_DOWN)
que.send_keys(Keys.RETURN)

Related Questions In Selenium

0 votes
2 answers

Python: Using an existing google chrome profile with selenium chrome web driver

The problem is with the string "C:\Users\Eric\Desktop\beeline.txt" Here, \U starts an ...READ MORE

answered May 24, 2019 in Selenium by shinio llahsra
13,037 views
+1 vote
2 answers

Python with Selenium issue: “Chrome is being controlled by automated test software”

from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("useAutomationExtension", False) chrome_options.add_experimental_option("excludeSwitches",["enable-automation"]) driver ...READ MORE

answered Apr 20, 2020 in Selenium by Manoj
15,686 views
+2 votes
2 answers

How can I press ENTER key with the execute_script in selenium python?

The below code containing Keys.ENTER might just ...READ MORE

answered Mar 28, 2018 in Selenium by nsv999
• 5,500 points
27,272 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8, 2019 in Selenium by Anvi
• 14,150 points
15,709 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,458 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,019 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,134 views
0 votes
2 answers

How to use Selenium with Python?

Hey Shubham, you can checkout this thread ...READ MORE

answered Aug 26, 2019 in Selenium by Abha
• 28,140 points
1,110 views
+2 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP