如何优化Selenium-WebDriver代理认证,减少初始未授权请求?
如何解决Selenium通过认证Squid代理时初始两次未认证请求的问题
我正在使用selenium-webdriver npm包,通过带认证的Squid代理访问页面,相关信息已脱敏,代码示例如下:
'use strict'; const { Builder } = require('selenium-webdriver'), proxy = require('selenium-webdriver/proxy'), proxyAddr = '<IP:PORT>', chrome = require('selenium-webdriver/chrome'); (async () => { try { const chromeOptions = new chrome.Options(), driverProxyConfig = proxy.manual({ http: proxyAddr, https: proxyAddr, bypass: ['accounts.google.com'] }); chromeOptions.setProxy(driverProxyConfig); const driver = await new Builder() .forBrowser('chrome') .setChromeOptions(chromeOptions) .build(), pageCdpConnection = await driver.createCDPConnection('page'); await driver.register('<username>', '<password>', pageCdpConnection); await driver.get('https://www.selenium.dev/'); await driver.sleep(10_000); await driver.quit(); } catch (err) { console.error(err); } })();
使用的软件版本
- Node.js: 14.21.1(需固定使用v14)
- Chromedriver: 111.0.5563.6
- Chrome: 111.0.5563.64
selenium-webdriver: 4.8.1- Mac OS 13.2(Ventura),M2
运行上述脚本(已填入正确的代理地址和凭证)后,查看Squid访问日志,发现会话初始总会出现至少两次连续的未认证请求(本案例中目标为www.selenium.dev),日志示例如下(已脱敏):
2023-03-20 10:59:51,580 0 <client-ip> TCP_DENIED/407 4284 CONNECT www.selenium.dev:443 - HIER_NONE/- text/html 2023-03-20 10:59:51,584 0 <client-ip> TCP_DENIED/407 4284 CONNECT www.selenium.dev:443 - HIER_NONE/- text/html 2023-03-20 10:59:51,954 1 <client-ip> TCP_TUNNEL/200 39 CONNECT cdn.jsdelivr.net:443 <username> HIER_DIRECT/146.75.121.229 - 2023-03-20 10:59:52,017 59 <client-ip> TCP_TUNNEL/200 4890 CONNECT fonts.googleapis.com:443 <username> HIER_DIRECT/142.250.186.42 - 2023-03-20 10:59:52,017 60 <client-ip> TCP_TUNNEL/200 4891 CONNECT fonts.googleapis.com:443 <username> HIER_DIRECT/142.250.186.42 - 2023-03-20 10:59:52,438 312 <client-ip> TCP_TUNNEL/200 6586 CONNECT www.paypal.com:443 <username> HIER_DIRECT/146.75.121.21 - 2023-03-20 10:59:52,750 97 <client-ip> TCP_TUNNEL/200 6505 CONNECT www.paypalobjects.com:443 <username> HIER_DIRECT/192.229.221.25 - 2023-03-20 11:00:02,801 10908 <client-ip> TCP_TUNNEL/200 39117 CONNECT cdn.jsdelivr.net:443 <username> HIER_DIRECT/146.75.121.229 - 2023-03-20 11:00:02,801 10674 <client-ip> TCP_TUNNEL/200 5150 CONNECT www.netlify.com:443 <username> HIER_DIRECT/3.74.73.235 - 2023-03-20 11:00:02,808 10314 <client-ip> TCP_TUNNEL/200 3455 CONNECT www.netlify.com:443 <username> HIER_DIRECT/3.74.73.235 - 2023-03-20 11:00:02,808 10860 <client-ip> TCP_TUNNEL/200 9388 CONNECT fonts.googleapis.com:443 <username> HIER_DIRECT/142.250.186.42 - 2023-03-20 11:00:02,808 11188 <client-ip> TCP_TUNNEL/200 572173 CONNECT www.selenium.dev:443 <username> HIER_DIRECT/185.199.110.153 - 2023-03-20 11:00:02,809 10683 <client-ip> TCP_TUNNEL/200 6651 CONNECT plausible.io:443 <username> HIER_DIRECT/169.150.247.34 - 2023-03-20 11:00:02,809 10939 <client-ip> TCP_TUNNEL/200 43293 CONNECT code.jquery.com:443 <username> HIER_DIRECT/69.16.175.42 - 2023-03-20 11:00:02,809 10683 <client-ip> TCP_TUNNEL/200 32814 CONNECT fonts.gstatic.com:443 <username> HIER_DIRECT/142.250.186.163 - 2023-03-20 11:00:02,809 10683 <client-ip> TCP_TUNNEL/200 8110 CONNECT www.paypal.com:443 <username> HIER_DIRECT/146.75.121.21 - 2023-03-20 11:00:02,809 10161 <client-ip> TCP_TUNNEL/200 10864 CONNECT www.paypalobjects.com:443 <username> HIER_DIRECT/192.229.221.25 -
我的场景要求日志中最多只能出现一次初始未认证请求,请问是否有实现该目标的方法?
内容的提问来源于stack exchange,提问作者Deimantas Bunys
相关产品推荐
相关产品推荐

