selenium报错:urllib3.exceptions.ProxySchemeUnknown: Proxy URL had no scheme, should start with http:// or https://

最近用Python+selenium做了一个自动化工具,但由于本地开启了全局代理,在发起请求的时候返回了上面的报错,经过一番研究,终于解决了这个问题。

报错部分的代码:

1
2
3
4
5
6
7
8
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
url = ''
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # 无界面模式
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(service=ChromeService(driver_path), options=options)
driver.get(url)

研究发现是作者要请求的链接不能走代理,所以解决方法就清晰了:

1. 关闭全局代理: 把代理关了,报错自然就没了,但是作者需要代理终端,所以全局代理还不能关。

2. 修改代理: 可以把要请求的连接的域名加入到代理的排除列表里,理论上可行,但是失败了,有意思的是全局代理下失败了,但是在规则代理下生效了,不知道是不是规则代理的锅,但是作者没有时间去研究了,所以只能用下面的方法。

3. 改代码: 这个应该算是最方便的做法了,只需要在上面的代码里加一行就行,修改后的代码如下:

1
2
3
4
5
6
7
8
9
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
url = ''
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # 无界面模式
options.add_argument("--disable-blink-features=AutomationControlled")
options.ignore_local_proxy_environment_variables()  # 忽略环境变量中的代理设置
driver = webdriver.Chrome(service=ChromeService(driver_path), options=options)
driver.get(url)

最后作者用的是方法3,完美解决,有时间在研究研究终端代理排除的问题,期待更新……