安装
npm init
npm install selenium-webdriver
main.js
const { Builder, By, until } = require("selenium-webdriver");
var driver = new Builder().forBrowser("chrome").build();
// 调用 WebDriver 对象的get方法 让浏览器打开指定网址
driver.get("https://www.yunlogin.com/");
driver.findElement(By.xpath("//nav/ul/li[5]")).click();
driver.wait(until.titleIs("指纹浏览器下载-防关联浏览器多开-电商浏览器-Yunlogin"), 10000);
driver.quit();
运行
node main.js
远程调试
debuggerAddress 模式
npm init
npm install selenium-webdriver
npm i axios
main.js
const { Builder, By, until } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const axios = require("axios");
/**
* 启动子浏览器
* @param {string} browserid 环境id
*/
async function start(browserid) {
var res = await axios.get(
`http://127.0.1:50213/api/v2/browser/start?account_id=${browserid}`
);
const { code, data } = res.data;
if (code === 0) {
const service = new chrome.ServiceBuilder(data.webdriver)
const options = new chrome.Options();
service.addArguments('--disable-build-check')
options.setChromeBinaryPath(data.webdriver)
options.addArguments(`--remote-debugging-port=${data.debuggingPort}`); // 远程调试端口
options.debuggerAddress(data.ws.selenium)
const driver = new Builder()
.forBrowser("chrome")
.setChromeOptions(options)
.setChromeService(service)
.build();
await driver.get("https://www.yunlogin.com/");
driver.findElement(By.xpath("//nav/ul/li[5]")).click();
driver.wait(
until.titleIs("指纹浏览器下载-防关联浏览器多开-电商浏览器-Yunlogin"),
10000
);
}
}
start("152829437d878d7a0e************");
运行
node main.js