使用 Selenium 和 Python 搜索 Google
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24598648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Searching Google with Selenium and Python
提问by dimensive
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:
我无法通过 Selenium 进行简单的谷歌搜索,但我相信我做得对。我试图遵循 Selenium 文档,但我认为该问题可能是由 python 或 selenium 安装不当引起的。我对python的了解很少。这是我的代码:
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.
如果您需要任何其他信息,我将很乐意分享。先感谢您。
采纳答案by Padraic Cunningham
Your finally
block will be executed whether there has been an exception or not. So browser.quit()
is always executed.
finally
无论是否有异常,你的 块都会被执行。所以 browser.quit()
总是被执行。
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 docson `waits.
关于“等待”的硒文档。
回答by SiKing
driver.quit()
terminates the session at that point. Try this:
driver.quit()
在该点终止会话。尝试这个:
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.
你提到你对 Python 的了解很少。选择一种您已经熟悉的语言,或者先通过一些 Python 教程来熟悉它可能是一个好主意。