将代理与 Python 一起使用时,Firefox selenium 会卡住
Firefox selenium gets stuck when using proxy with Python
这是我的代码,但是当我启动 运行 它时,Firefox 打开但没有导航到该站点。如果我尝试手动输入 URL,它仍然没有显示代理的 IP 地址。
from selenium import webdriver
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities["marionette"] = True
PROXY = "40.91.94.165:3128"
firefox_capabilities["proxy"] = {
"proxyType": "MANUAL",
"httpProxy": PROXY,
"ftpProxy": PROXY,
"sslProxy": PROXY,
}
driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver = webdriver.Firefox()
driver.get("https://wtfismyip.com")
这是我尝试停止它时遇到的错误
^CTraceback (most recent call last):
File "/Users/****/Developer/python/proxy.py", line 16, in <module>
driver = webdriver.Firefox(capabilities=firefox_capabilities)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 79, in request
return self.request_encode_body(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/poolmanager.py", line 330, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1347, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 268, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
我的解决方案与您的解决方案不同。
当我想做类似的事情时,我正在使用 Chrome,但我尝试使用 Firefox 来帮助你。
所以我的解决方案需要安装另外 2 个包。第一个是 webdriver_manager
,你可以像这样安装它(确保完全按照我这里的方式安装):pip install -U webdriver-manager
第二个是 selenium-wire
你可以这样安装它 pip install selenium-wire
from seleniumwire import webdriver
from webdriver_manager.firefox import GeckoDriverManager
PROXY = "40.91.94.165:3128"
proxy = {
'proxy': {
'http': "http://" + f"{PROXY}",
'https': "http://" + f"{PROXY}"
}
}
options = webdriver.FirefoxOptions()
options.headless = False
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(),
options=options, seleniumwire_options=proxy)
driver.get("https://wtfismyip.com")
这是我的代码,但是当我启动 运行 它时,Firefox 打开但没有导航到该站点。如果我尝试手动输入 URL,它仍然没有显示代理的 IP 地址。
from selenium import webdriver
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities["marionette"] = True
PROXY = "40.91.94.165:3128"
firefox_capabilities["proxy"] = {
"proxyType": "MANUAL",
"httpProxy": PROXY,
"ftpProxy": PROXY,
"sslProxy": PROXY,
}
driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver = webdriver.Firefox()
driver.get("https://wtfismyip.com")
这是我尝试停止它时遇到的错误
^CTraceback (most recent call last):
File "/Users/****/Developer/python/proxy.py", line 16, in <module>
driver = webdriver.Firefox(capabilities=firefox_capabilities)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 79, in request
return self.request_encode_body(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/poolmanager.py", line 330, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1347, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 268, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
我的解决方案与您的解决方案不同。 当我想做类似的事情时,我正在使用 Chrome,但我尝试使用 Firefox 来帮助你。
所以我的解决方案需要安装另外 2 个包。第一个是 webdriver_manager
,你可以像这样安装它(确保完全按照我这里的方式安装):pip install -U webdriver-manager
第二个是 selenium-wire
你可以这样安装它 pip install selenium-wire
from seleniumwire import webdriver
from webdriver_manager.firefox import GeckoDriverManager
PROXY = "40.91.94.165:3128"
proxy = {
'proxy': {
'http': "http://" + f"{PROXY}",
'https': "http://" + f"{PROXY}"
}
}
options = webdriver.FirefoxOptions()
options.headless = False
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(),
options=options, seleniumwire_options=proxy)
driver.get("https://wtfismyip.com")