我想使用Selenium测试Chrome扩展程序。 Chrome扩展程序在后台进行其余的API调用。但是Selenium chrome阻止了远程连接。
错误
chrome_1 | Only local connections are allowed.
chrome_1 | Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
chrome_1 | [1553232680.777][SEVERE]: bind() failed: Cannot assign requested address (99)
chrome_1 | 05:31:25.002 INFO [ProtocolHandshake.createSession] - Detected dialect: OSS
要加载扩展程序的代码
var sw = require('selenium-webdriver');
const path = require('path');
const extensionPath = path.resolve( __dirname, './assets/SampleExtension' );
var chromeOptions = sw.Capabilities.chrome();
chromeOptions.set("chromeOptions", {"args": ['--load-extension='+extensionPath, "--whitelisted-ips", "disable-gpu", "disable-infobars", "no-sandbox", "allow-insecure-localhost"]});
let driver = new sw.Builder().forBrowser('chrome')
.setChromeOptions(chromeOptions)
.usingServer('http://chrome:4444/wd/hub')
.build();
driver.get('https://samplewebsite.com/').then(() => {
console.log('web page open ')
});
答案 0 :(得分:0)
尝试一下!
var os = require('os');
var webdriver = require('selenium-webdriver');
var path = require('path');
function getLocalBrowser() {
var chrome = require('selenium-webdriver/chrome'),
exeName = {
"Linux": "bin/linux/chromedriver",
"Darwin": "bin/mac/chromedriver",
"Windows_NT": "bin/windows/chromedriver.exe"
},
exeLocation = path.resolve(__dirname, path.relative(__dirname,
exeName[os.type()])),
service = new chrome.ServiceBuilder(exeLocation).build(),
driver = new chrome.Driver(webdriver.Capabilities.chrome(), service),
driverWindow = driver.manage().window();
driverWindow.maximize();
return driver;
}
module.exports = {
getLocalBrowser: getLocalBrowser
}