【问题标题】:SOCKS error using Selenium with Python 2.7x在 Python 2.7x 中使用 Selenium 时出现 SOCKS 错误
【发布时间】:2015-03-27 22:00:05
【问题描述】:

我正在使用 Python 2.7x,并且正在尝试与使用 Selenium 的网站进行交互。每次我的代码到达我的代码的这个方面时,我都会收到以下SOCKS 错误。我正在使用 Firefox v37 和 Selenium v​​2.45.0

Traceback (most recent call last):
  File "C:\Users\Dell\Desktop\test.py", line 13, in <module>
    get_password("www.facebook.com")
  File "C:\Users\Dell\Desktop\test.py", line 6, in get_password
    driver.get(album)
  File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.45.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 187, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.45.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 175, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.45.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: f.QueryInterface is not a function
Stacktrace:
    at FirefoxDriver.prototype.get (file:///c:/users/dell/appdata/local/temp/tmpyuwka_/extensions/fxdriver@googlecode.com/components/driver-component.js:10132)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/dell/appdata/local/temp/tmpyuwka_/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/dell/appdata/local/temp/tmpyuwka_/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
    at DelayedCommand.prototype.execute/< (file:///c:/users/dell/appdata/local/temp/tmpyuwka_/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)

这是我的代码:

from selenium.webdriver.common.keys import Keys
from selenium import webdriver

def get_password(album):
    driver = webdriver.Firefox()
    driver.get(album)
    elem = driver.find_element_by_name("pwd")
    elem.send_keys(password)
    elem.send_keys(Keys.RETURN)

    driver.quit()

get_password("www.facebook.com")

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    我怀疑这是一个兼容性问题升级 selenium 到 2.45(目前是最新的):

    pip install --upgrade selenium
    

    并且,只是对代码的快速补充说明:为什么不在循环之外实例化并退出驱动程序实例 - 这会对性能产生积极影响:

    driver = webdriver.Firefox()
    for album in albums:
        for value in content_list:
            ...
    driver.quit()
    

    更新:您需要提供一个 URL 架构,替换:

    get_password("www.facebook.com")
    

    与:

    get_password("https://www.facebook.com")
    

    【讨论】:

    • 谢谢@alecxe。我按照先前关于 Selenium 错误的建议运行 Firefox v31,但我现在将 v37 与 Selenium 2.45.0 一起使用。我现在遇到了一个新错误。原帖已修改
    • 谢谢@alexce。现在效果很好,所以我知道不存在兼容性问题。不过,我的原始代码仍然有错误。我认为该错误与“文件”C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\common\utils.py 有关,第 43 行,在 is_connectable socket_.connect(("127.0.0.1", port))" 中。上面的代码是独立工作的,但嵌入在我的主代码中的相同代码不起作用。我确实在我的代码中通过 tor 查询了一些 URL,所以我想知道这是否会导致问题。话虽如此,当我在上面运行时,tor浏览器仍在运行
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2020-07-15
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多