使用selenium python打开两个chrome webdriver实例(带和不带代理) &g



我试图用selenium python创建两个chrome webdriver实例。第一个使用代理,第二个不使用代理。我面临的问题是,在第二个实例中打开的URL不加载(因为它使用代理)。我已经实现了两个不同的实例,但我仍然得到这个错误。这是我目前为止写的内容:

import os, tempfile
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver
# Set up Selenium with proxy and incognito mode
def setup_selenium_with_proxy(proxy):
proxy_config = Proxy()
proxy_config.proxy_type = ProxyType.MANUAL
proxy_config.http_proxy = f"{proxy['ip']}:{proxy['port']}"
proxy_config.ssl_proxy = f"{proxy['ip']}:{proxy['port']}"
capabilities = webdriver.DesiredCapabilities.CHROME
proxy_config.add_to_capabilities(capabilities)
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_argument("--log-level=3")
options.add_experimental_option("excludeSwitches", ["enable-automation", "enable-logging"])
# Create a separate user data directory for the proxy browser
user_data_dir_proxy = os.path.join(tempfile.gettempdir(), "selenium_proxy_profile")
if not os.path.exists(user_data_dir_proxy):
os.makedirs(user_data_dir_proxy)
options.add_argument(f"--user-data-dir={user_data_dir_proxy}")
driver = webdriver.Chrome(options=options, desired_capabilities=capabilities)
return driver
# Set up Selenium without proxy and incognito mode
def setup_selenium_without_proxy():
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_argument("--log-level=3")
options.add_experimental_option("excludeSwitches", ["enable-automation", "enable-logging"])
# Create a separate user data directory for the non-proxy browser
user_data_dir_no_proxy = os.path.join(tempfile.gettempdir(), "selenium_no_proxy_profile")
if not os.path.exists(user_data_dir_no_proxy):
os.makedirs(user_data_dir_no_proxy)
options.add_argument(f"--user-data-dir={user_data_dir_no_proxy}")
driver = webdriver.Chrome(options=options)
return driver

我的主要功能首先与代理驱动程序一起工作,当我调用第二个无代理驱动程序时,网站不工作并显示网站无法到达net::ERR_TUNNEL_CONNECTION_FAILED.

def main():
url = "http://example.com"

# Read proxies from 'http.txt' file
with open('http.txt', 'r') as file:
proxy_list = [{'ip': proxy.split(':')[0], 'port': proxy.split(':')[1].strip()} for proxy in file.readlines()]
at_least_one_working_proxy = False
proxy_index = 0
while proxy_index < len(proxy_list):
proxy = proxy_list[proxy_index]
driver = setup_selenium_with_proxy(proxy)
driver.set_page_load_timeout(20)
try:
driver.get(url)

# code

new_driver = setup_selenium_without_proxy()
new_driver.set_page_load_timeout(20)
except TimeoutException:
print(f"Failed to open url using proxy {proxy['ip']}:{proxy['port']} within 20 seconds")
except WebDriverException as e:
print(f"WebDriverException encountered while using proxy {proxy['ip']}:{proxy['port']}: {e}")

将以下行添加到without_proxy函数中:

options.add_argument('--no-proxy-server')