【问题标题】:Configure proxy settings Selenium Firefox配置代理设置 Selenium Firefox
【发布时间】:2017-07-21 06:04:43
【问题描述】:

我已经尝试了大约 2 天来使用 selenium 和代理服务器,但仍然无法做到。我已经尝试过 selenium 网站上的方法,复制和粘贴,但没有奏效。我已经尝试了 Stackoverflow 的所有答案,但都没有奏效,我得到的最接近的是加载页面,但我的 IP 保持不变。

这是我最近尝试的代码:

#!/usr/bin/env python

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities



HOST = "144.217.31.225"
PORT = "3128"
def my_proxy(PROXY_HOST,PROXY_PORT):
        fp = webdriver.FirefoxProfile()
        # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
        print PROXY_PORT
        print PROXY_HOST
        fp.set_preference("network.proxy.type", 1)
        fp.set_preference("network.proxy.http",PROXY_HOST)
        fp.set_preference("network.proxy.http_port",int(PROXY_PORT))
        fp.set_preference("general.useragent.override","whater_useragent")
        fp.update_preferences()
        return webdriver.Firefox(firefox_profile=fp)

browser = my_proxy(HOST,PORT)
browser.get('https://www.google.com')

search=browser.find_element_by_xpath("//input[@title='Search']")
search.send_keys('my ip')

这是我收到的错误:

3128
144.217.31.225
Traceback (most recent call last):
  File "./script.py", line 32, in <module>
    search=browser.find_element_by_xpath("//input[@title='Search']")
  File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 313, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
    'value': value})['value']
  File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@title='Search']

它设法加载页面,但无法在搜索栏中输入文本。我手动接管并在搜索栏中输入“我的 IP”,但 IP 没有更改。

【问题讨论】:

  • 我试过那个
  • 你的错误告诉 selenium 找不到元素,你应该等到它加载,例如 sleep(5) 。尝试添加此参数("network.proxy.ssl", proxyHost) ("network.proxy.ssl_port", proxyPort),因为您连接到 https
  • 您的问题与代理无关吗?
  • @SAZ 谢谢你这样做了,或者尽管谷歌认为我是一个机器人很合适,我猜这是真的。

标签: python selenium firefox proxy


【解决方案1】:

以下代码有效,或者尽管谷歌不喜欢我使用 selenium 代理这一事实,这就是我将地址更改为 whatsmyipaddress.com 的原因。

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from time import sleep


HOST = "144.217.31.225"
PORT = "3128"
def my_proxy(PROXY_HOST,PROXY_PORT):
        fp = webdriver.FirefoxProfile()
        # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
        print PROXY_PORT
        print PROXY_HOST
        fp.set_preference("network.proxy.type", 1)
        fp.set_preference("network.proxy.http",PROXY_HOST)
        fp.set_preference("network.proxy.http_port",int(PROXY_PORT))
        fp.set_preference("network.proxy.ssl",PROXY_HOST)
        fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT))
        fp.set_preference("general.useragent.override","whater_useragent")
        fp.update_preferences()
        return webdriver.Firefox(firefox_profile=fp)



browser = my_proxy(HOST,PORT)
browser.get('http://whatismyipaddress.com/')

sleep(5)

search=browser.find_element_by_xpath("//input[@title='Search']")
search.send_keys('my ip')

感谢@SAZ 的解决方案。

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多