跑步 Selenium Webdriver 通过代理 Python

我正在尝试运行脚本 Selenium Webdriver 在 Python 执行一些主要任务。 当我开始它时,我可以让机器人完美地工作 Selenium IDE inteface /即:当只是强迫时 GUI 重复我的行为/. 但是,当我将代码导出为脚本时 Python 并尝试从命令行,浏览器执行它 Firefox 将打开,但永远无法访问开始 URL /错误返回命令行,程序停止/. 无论哪个网站等,这都发生在我身上。 我正在努力访问。

我在这里打开了一个非常简单的演示目的代码。 我认为我不正确地打开代理代码部分,因为返回错误似乎是由代理服务器生成的。

任何帮助都非常感激。

下面的代码简单地打开。 www.google.ie 和搜索单词 "selenium". 对我来说,它打开了一个空浏览器 firefox 并停止。


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from selenium.webdriver.common.proxy import *

class Testrobot2/unittest.TestCase/:
def setUp/self/:

myProxy = "[url=http://149.215.113.110:70"]http://149.215.113.110:70"[/url]

proxy = Proxy/{
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy':''}/

self.driver = webdriver.Firefox/proxy=proxy/
self.driver.implicitly_wait/30/
self.base_url = "[url=https://www.google.ie/"]https://www.google.ie/"[/url]
self.verificationErrors = []
self.accept_next_alert = True

def test_robot2/self/:
driver = self.driver
driver.get/self.base_url + "/#gs_rn=17&gs_ri=psy-ab&suggest=p&cp=6&gs_id=ix&xhr=t&q=selenium&es_nrs=true&pf=p&output=search&sclient=psy-ab&oq=seleni&gs_l=&pbx=1&bav=on.2,or.r_qf.&bvm=bv.47883778,d.ZGU&fp=7c0d9024de9ac6ab&biw=592&bih=665"/
driver.find_element_by_id/"gbqfq"/.clear//
driver.find_element_by_id/"gbqfq"/.send_keys/"selenium"/

def is_element_present/self, how, what/:
try: self.driver.find_element/by=how, value=what/
except NoSuchElementException, e: return False
return True

def is_alert_present/self/:
try: self.driver.switch_to_alert//
except NoAlertPresentException, e: return False
return True

def close_alert_and_get_its_text/self/:
try:
alert = self.driver.switch_to_alert//
alert_text = alert.text
if self.accept_next_alert:
alert.accept//
else:
alert.dismiss//
return alert_text
finally: self.accept_next_alert = True

def tearDown/self/:
self.driver.quit//
self.assertEqual/[], self.verificationErrors/

if __name__ == "__main__":
unittest.main//
已邀请:

快网

赞同来自:

为我工作 /看起来像代码 @Amey 和 @user4642224, 但较短/:


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

prox = Proxy//
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"

capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities/capabilities/

driver = webdriver.Chrome/desired_capabilities=capabilities/

快网

赞同来自:

怎么样的东西


PROXY = "149.215.113.110:70"

webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
}

# you have to use remote, otherwise you'll have to code it yourself in python to
driver = webdriver.Remote/"[url=http://localhost:4444/wd/hub"]http://localhost:4444/wd/hub"[/url], webdriver.DesiredCapabilities.FIREFOX/


您可以了解更多关于它的信息。
http://docs.seleniumhq.org/doc ... proxy
.

喜特乐

赞同来自:

我的决定:


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/


然后打电话给你的代码:


my_proxy/PROXY_HOST,PROXY_PORT/


我有这个代码的问题,因为我将字符串传递为端口 #:


PROXY_PORT="31280"


这非常重要:


int/"31280"/


您必须传递整数而不是字符串,否则您的个人资料 firefox 它不会安装在正确的端口上,通过代理的连接将无法工作。

小明明

赞同来自:

尝试配置代理 sock5. 我遇到了同样的问题,它通过代理解决了 socks


def install_proxy/PROXY_HOST,PROXY_PORT/:
fp = webdriver.FirefoxProfile//
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.https",PROXY_HOST/
fp.set_preference/"network.proxy.https_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/"network.proxy.ftp",PROXY_HOST/
fp.set_preference/"network.proxy.ftp_port",int/PROXY_PORT//
fp.set_preference/"network.proxy.socks",PROXY_HOST/
fp.set_preference/"network.proxy.socks_port",int/PROXY_PORT//
fp.set_preference/"general.useragent.override","Mozilla/5.0 /Macintosh; Intel Mac OS X 10_9_3/ AppleWebKit/537.75.14 /KHTML, like Gecko/ Version/7.0.3 Safari/7046A194A"/
fp.update_preferences//
return webdriver.Firefox/firefox_profile=fp/


然后打电话
install_proxy / ip , port /

来自您的计划。

卫东

赞同来自:

如果有人正在寻找这样的解决方案 :


from selenium import webdriver
PROXY = "YOUR_PROXY_ADDRESS_HERE"
webdriver.DesiredCapabilities.FIREFOX['proxy']={
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"autodetect":False
}
driver = webdriver.Firefox//
driver.get/'http://www.whatsmyip.org/'/

涵秋

赞同来自:

支票代理。 这是一个全新的脚本。 python 在与样本方案的链接中,Mikhail Marcinyuk。


# Load webdriver
from selenium import webdriver

# Load proxy option
from selenium.webdriver.common.proxy import Proxy, ProxyType

# Configure Proxy Option
prox = Proxy//
prox.proxy_type = ProxyType.MANUAL

# Proxy IP & Port
prox.http_proxy = “0.0.0.0:00000”
prox.socks_proxy = “0.0.0.0:00000”
prox.ssl_proxy = “0.0.0.0:00000”

# Configure capabilities
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities/capabilities/

# Configure ChromeOptions
driver = webdriver.Chrome/executable_path='/usr/local/share chromedriver',desired_capabilities=capabilities/

# Verify proxy ip
driver.get/"[url=http://www.whatsmyip.org/"/]http://www.whatsmyip.org/"/[/url]

江南孤鹜

赞同来自:

尝试设置 FirefoxProfile


from selenium import webdriver
import time


"Define Both ProxyHost and ProxyPort as String"
ProxyHost = "54.84.95.51"
ProxyPort = "8083"



def ChangeProxy/ProxyHost ,ProxyPort/:
"Define Firefox Profile with you ProxyHost and ProxyPort"
profile = webdriver.FirefoxProfile//
profile.set_preference/"network.proxy.type", 1/
profile.set_preference/"network.proxy.http", ProxyHost /
profile.set_preference/"network.proxy.http_port", int/ProxyPort//
profile.update_preferences//
return webdriver.Firefox/firefox_profile=profile/


def FixProxy//:
""Reset Firefox Profile""
profile = webdriver.FirefoxProfile//
profile.set_preference/"network.proxy.type", 0/
return webdriver.Firefox/firefox_profile=profile/


driver = ChangeProxy/ProxyHost ,ProxyPort/
driver.get/"[url=http://whatismyipaddress.com"/]http://whatismyipaddress.com"/[/url]

time.sleep/5/

driver = FixProxy//
driver.get/"[url=http://whatismyipaddress.com"/]http://whatismyipaddress.com"/[/url]


这个计划

测试

如何 Windows 8, 和 Mac OSX. 如果您正在使用 Mac OSX 如果你没有更新 selenium, 然后你可能会遇到
selenium.common.exceptions.WebDriverException

. 如果是,请在更新后重复尝试 selenium


pip install -U selenium

知食

赞同来自:

上面指定的结果可能是正确的,但不适用于最后一个版本 webdriver.
这是我对上述问题的决定。 简单而甜蜜


http_proxy = "ip_addr:port"
https_proxy = "ip_addr:port"

webdriver.DesiredCapabilities.FIREFOX['proxy']={
"httpProxy":http_proxy,
"sslProxy":https_proxy,
"proxyType":"MANUAL"
}

driver = webdriver.Firefox//


OR


http_proxy = "[url=http://ip:port"]http://ip:port"[/url]
https_proxy = "[url=https://ip:port"]https://ip:port"[/url]

proxyDict = {
"http" : http_proxy,
"https" : https_proxy,
}

driver = webdriver.Firefox/proxy=proxyDict/

小姐请别说爱

赞同来自:

上面的答案
https://coderoad.ru/17988821/
要么不适合我 Selenium 3.14 和 Firefox 68.9 在 Linux, 或者太复杂了。 我需要使用配置 WPAD, 有时用于代理 /在 VPN/, 有时不是。 在研究代码后,我结束了::


from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

proxy = Proxy/{'proxyAutoconfigUrl': 'http://wpad/wpad.dat'}/
profile = FirefoxProfile//
profile.set_proxy/proxy/
driver = webdriver.Firefox/firefox_profile=profile/


代理服务器初始化建立 proxyType 在 ProxyType.PAC /自动配置从 A URL/ 作为副作用。

他还与自动检测合作 Firefox, 使用:


from selenium.webdriver.common.proxy import ProxyType

proxy = Proxy/{'proxyType': ProxyType.AUTODETECT}/


但我认为它会像内在一样工作 URLs /没有代理/, 所以用外部 /突出/ 时尚 WPAD. 类似的代理服务器设置必须用于手动配置。 可以在代码中看到可能的代理服务器设置。
https://www.selenium.dev/selen ... .html
.

请注意,Proxy对象的直接传输为
proxy=proxy

驱动器不起作用 - 它被接受,但忽略了 /obsolesh时必须有警告,但在我的情况下我认为这一点 Behavior 吞下它/.

诸葛浮云

赞同来自:

如图所示 @Dugini,, 一些
https://github.com/SeleniumHQ/ ... ities
被删除了。 最大限度:


webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":[],
"proxyType":"MANUAL"
}

八刀丁二

赞同来自:

它为我工作,允许使用页岩浏览器,只需调用传输代理的方法。


def setProxy/proxy/:
options = Options//
options.headless = True
#options.add_argument/"--window-size=1920,1200"/
options.add_argument/"--disable-dev-shm-usage"/
options.add_argument/"--no-sandbox"/
prox = Proxy//
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = proxy
prox.ssl_proxy = proxy
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities/capabilities/
return webdriver.Chrome/desired_capabilities=capabilities, options=options, executable_path=DRIVER_PATH/

冰洋

赞同来自:

尝试运行服务 tor, 将以下函数添加到您的代码中。


def connect_tor/port/:

socks.set_default_proxy/socks.PROXY_TYPE_SOCKS5, '127.0.0.1', port, True/
socket.socket = socks.socksocket

def main//:

connect_tor//
driver = webdriver.Firefox//

要回复问题请先登录注册