When you try to export a Selenium Webdriver script as a Python script and execute it from the command line, you may encounter the problem of using An error occurred in the case of proxy. This article aims to address this issue by providing a solution for running scripts efficiently using a proxy.
To run Selenium Webdriver using a proxy, you need to configure the DesiredCapabilities class of Selenium WebDriver. The following steps will guide you through the process:
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.sock_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"
# 构建 DesiredCapabilities
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
# 使用 DesiredCapabilities 实例化驱动程序
driver = webdriver.Chrome(desired_capabilities=capabilities)
# 使用该驱动程序进行自动化任务
Using the above method, you can easily run Selenium Webdriver scripts using agents in Python. By effectively configuring the DesiredCapabilities class, you can avoid proxy-related errors and ensure that scripts can access restricted or geographically restricted websites.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3