简体   繁体   English

使用 Selenium 和 Python 搜索 Google

[英]Searching Google with Selenium and Python

I am not able to do a simple google search through Selenium, although I believe I am doing it correctly.我无法通过 Selenium 进行简单的谷歌搜索,尽管我相信我做得正确。 I attempted to follow the Selenium documentation, but I believe the issue might be caused by an improper install of python or selenium.我试图遵循 Selenium 文档,但我认为该问题可能是由于 python 或 selenium 安装不当引起的。 I have little python knowledge.我对python知之甚少。 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.先感谢您。

Your finally block will be executed whether there has been an exception or not.无论是否有异常,都会执行您的finally块。 So browser.quit() is always executed.所以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 docs on `waits. `waits 上的selenium 文档

driver.quit() terminates the session at that point. driver.quit()在此时终止会话。 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.您提到您对python的了解很少。 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 教程来熟悉它可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM