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.
In order, to perform a search, we have to first land on the Google page with the help of the get method. Then identify the search edit box with the help of any of the locators like id, name, class, xpath, tagname, or css. Then enter the keyword in the search box with the help of the sendKeys method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With