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 in Selenium by ADS
• 280 points
720 views

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 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.

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 23 in Selenium by shinio llahsra
3,420 views
+1 vote
1 answer
+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 27, 2018 in Selenium by nsv999
• 5,430 points
8,656 views
0 votes
3 answers

Scripting Languages used with Selenium tool for Automation Testing

The biggest advantage of Selenium is that ...READ MORE

answered Aug 28 in Selenium by Abha
• 27,200 points
310 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

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

answered May 8 in Selenium by Anvi
• 13,960 points
279 views
0 votes
1 answer

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,430 points
1,181 views
0 votes
1 answer
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,170 points
1,267 views
0 votes
2 answers

How to use Selenium with Python?

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

answered Aug 26 in Selenium by Abha
• 27,200 points
193 views
+2 votes
1 answer