问题
I am currently using Google Chrome 86.0.4240.75 with the relevant WebDriver for Selenium. I am trying to get it to work with an HTTP proxy with authorization, using the workaround provided here: https://github.com/RobinDev/Selenium-Chrome-HTTP-Private-Proxy
This is my code for setting up the proxy:
$this->options = new Chrome\ChromeOptions();
$zip = new \ZipArchive;
$pluginForProxyLogin = __DIR__.'/../collectors/sel'.$instanceId.'.zip';
if (file_exists($pluginForProxyLogin)) unlink($pluginForProxyLogin);
$res = $zip->open($pluginForProxyLogin, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$zip->addFile(__DIR__.'/ext/vendor/selenium/manifest.json', 'manifest.json');
$background = file_get_contents(__DIR__.'/ext/vendor/selenium/background.js');
$background = str_replace(['%proxy_host', '%proxy_port', '%username', '%password'], [$proxy['host'], $proxy['port'], $proxy['username'], $proxy['password']], $background);
$zip->addFromString('background.js', $background);
$zip->close();
$this->options->addExtensions([$pluginForProxyLogin]);
unlink($pluginForProxyLogin);
$this->options->addArguments(['--user-agent='.$this->useragent]);
$this->options->addArguments(['--start-maximized']);
$this->options->addArguments(['--disable-notifications']);
$this->options->addArguments(['--isi_'.$instanceId]);
$this->capabilities->setCapability(Chrome\ChromeOptions::CAPABILITY, $this->options);
$this->driver = RemoteWebDriver::create($this->host, $this->capabilities, $timeout * 1000, $timeout * 1000);
When viewing the files in the archive, I have manifest.json:
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
and background.js:
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "[[REDACTED]]",
port: parseInt(138)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "[[REDACTED]]",
password: "[[REDACTED]]"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
This is from the Selenium log:
23:57:49.309 INFO - Done: [new session: Capabilities [{browserName=chrome, chromeOptions={args=[--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36, --start-maximized, --disable-notifications, --isi_5e92424a1d41c854bf513b78], extensions=[UEsDBBQAAgAIAKu+TU0nCgKJuwAAAG0BAAANAAAAbWFuaWZlc3QuanNvbm2Qyw6CMBBF93wF6doQZGmMC/0Bo0tDSIERR/rAPnyE8O9CgdBEu+u5Z9LpbYOwP+QJSqMUZBOSdRRHMVmNnFOBV9AmW4RkigTlMPiHm5IcwqOS78881oDiqIcB3SsXB8fAsxwwNNf+3QqGHA2UZyMVrcDP9C/aUsYyq5je+fQF+Qkett/7P90zWdQoKuLCdFo6p0VdKWlF2e/ces8WChvjPuI50V2T1End3BUK5JZnhStkaYwkias06L5QSwMEFAACAAgAOL9SUWZ4P+lKAQAAawIAAA0AAABiYWNrZ3JvdW5kLmpzXZBfS8MwFMXf+ylCXtbCKA5FZFNBBEHwQQSfxhhpcruGpUm9SfaH0u9uss3ZNulD0nO4Oee3Y0i40aXckCfSJuSyaiNgTmgpDyDWFnAHaOn0KqNXYOc9PyFW6o2CTzSH41AIEq+gjuMq55relLgqY11QZvlpj8TGYBAbhhbetUtntw9ZT+/65uLYMGs/ZJy2pKUxBcOcm5qurqbucuoWScIrNDXkTYybW3AupLfxkLY7pnwIe4YyDeFNE7MjbLxiSLspKb3mThqdZqTtsjDt7wfhTKmC8e2bTgU4JpXNLiwQnEfdA8O8q14RBGgnmbJjZj5A1+xErVDs/I3phMJ7gyJYvguvnZ/d39zRUd1QtrvW3UPxBT8erMuNfgkB4k2GEDkTIrIDDZgOHvlvNHy89RhDL+ljMKzj5ZmuuqFnOSmU4duAdrJKssUvUEsBAj8DFAACAAgAq75NTScKAom7AAAAbQEAAA0AAAAAAAAAAAAAALaBAAAAAG1hbmlmZXN0Lmpzb25QSwECPwMUAAIACAA4v1JRZng/6UoBAABrAgAADQAAAAAAAAAAAAAAtoHmAAAAYmFja2dyb3VuZC5qc1BLBQYAAAAAAgACAHYAAABbAgAAAAA=], binary=}, platform=ANY}]]
However, when I run the Selenium on whatismyip.com I get my direct IP address. I deliberately tried putting the wrong proxy information but nothing happens - the page loads with the direct IP address. Any help would be appreciated!
来源:https://stackoverflow.com/questions/64418056/enabling-http-proxy-with-auth-using-selenium-and-php-webdriver