10

I am not able to do a simple google search through Selenium, although I believe I am doing it correctly. I attempted to follow the Selenium documentation, but I believe the issue might be caused by an improper install of python or selenium. I have little python knowledge. Here is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

browser = webdriver.Firefox()
browser.get('http://www.google.com')

try:
    element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "gbqfq")))
finally:
browser.quit()

search = browser.find_element_by_name('q')
search.send_keys("google search through python")

This is what terminal outputs.

 Mark-Kowalskys-iMac:~ markkowalsky$ cd '/Users/markkowalsky/Desktop/' && '/usr/bin/pythonw'  '/Users/markkowalsky/Desktop/searchGoogle.py'  && echo Exit status: $? && exit 1
Traceback (most recent call last):
  File "/Users/markkowalsky/Desktop/searchGoogle.py", line 14, in <module>
search = browser.find_element_by_name('q')
  File "/Library/Python/2.6/site-packages/selenium/webdriver/remote/webdriver.py", line 302, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
  File "/Library/Python/2.6/site-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
{'using': by, 'value': value})['value']
  File "/Library/Python/2.6/site-packages/selenium/webdriver/remote/webdriver.py", line 171, in execute
response = self.command_executor.execute(driver_command, params)
  File "/Library/Python/2.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 347, in execute
return self._request(command_info[0], url, body=data)
  File "/Library/Python/2.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 377, in _request
self._conn.request(method, parsed_url.path, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 874, in request
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 911, in _send_request
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 868, in endheaders
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 740, in _send_output
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 699, in send
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 683, in connect
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 512, in create_connection
socket.error: [Errno 61] Connection refused

If you need any other information I will gladly share. Thank you in advance.

1
  • You have an IndentationError in your finally block. Except from that I agree with the gist what Padraic Cunningham wrote. Commented Jul 6, 2014 at 19:02

2 Answers 2

23

Your finally block will be executed whether there has been an exception or not. So browser.quit() is always executed.

If you want to just search this script will do it for you.

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get('http://www.google.com')

search = browser.find_element_by_name('q')
search.send_keys("google search through python")
search.send_keys(Keys.RETURN) # hit return after you enter search text
time.sleep(5) # sleep for 5 seconds so you can see the results
browser.quit()

The selenium docs on `waits.

Sign up to request clarification or add additional context in comments.

3 Comments

I'd not call browser.quit() immediately after the results are displayed. But - of course - this depends on what user3810035 is going to achieve with this script.
@Sebastian, it is just an example of how to search and to use sleep to wait between calls.
quit() is not in finally as not indented. Still it's executed if no exception occurs.
0

driver.quit() terminates the session at that point. Try this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

browser = webdriver.Firefox()
browser.get('http://www.google.com')

WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "gbqfq")))

search = browser.find_element_by_name('q')
search.send_keys("google search through python")

browser.quit()

You mention that you have little python knowledge. It might be a good idea to either pick a language that you are already familiar with, or first got through some python tutorials to get yourself familiar with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.