nodejs selenium

nodejs selenium

https://www.selenium.dev/selenium/docs/api/javascript/index.html

 

驱动下载地址

https://github.com/mozilla/geckodriver/releases/

 

0.26.0版本驱动 支持chrome 71~75

 

firefox 0.26.0 支持目前最新的版本

将下载的驱动和文件放到一起或者加入path

 

截图百度搜索

const {Builder, By, Key, until, Options} = require("selenium-webdriver");
const fs = require("fs");
const firefox = require("selenium-webdriver/firefox");

function waitTime(n) {
  return new Promise(resolve => setTimeout(
    () => resolve()
    , n
  ))
}

(async function example() {
  let options = new firefox.Options()
  options.headless()
  let driver = await new Builder().forBrowser("firefox")
    .setFirefoxOptions(options)
    .build(options);
  try {
    await driver.get("http://www.baidu.com");
    await driver.findElement(By.id("kw")).sendKeys("webdriver", Key.RETURN);
    await driver.wait(until.titleIs("webdriver_百度搜索"), 5000)

    await waitTime(3000)
    let base64 = await driver.takeScreenshot()
    let b = Buffer.from(base64, "base64")
    fs.writeFileSync("./t.png", b)
  } finally {
    await driver.quit();
  }
})();

 

 

统计页面中节点总数

const {Builder, By, Key, until, Options} = require("selenium-webdriver");
const fs = require("fs");
const firefox = require("selenium-webdriver/firefox");

function waitTime(n) {
  return new Promise(resolve => setTimeout(
    () => resolve()
    , n
  ))
}

(async function example() {
  let options = new firefox.Options()
  options.headless()
  let driver = await new Builder().forBrowser("firefox")
    .setFirefoxOptions(options)
    .build(options);
  try {
    await driver.get("http://www.baidu.com");
    await driver.findElement(By.id("kw")).sendKeys("webdriver", Key.RETURN);
    await driver.wait(until.titleIs("webdriver_百度搜索"), 5000)
    let sums = []
    for (let i = 0; i < 3; i++) {
      await waitTime(2000)
      let sum = await driver.executeScript(
        `
let sum = 0
function dfs(dom) {
  if (dom.children.length) {
      [...dom.children].forEach(dfs)
  }
  sum++
};
dfs(document)
return sum
`
      )
      // console.log(sum)
      sums.push(sum)
    }
    console.log(sums)
  } finally {
    await driver.quit();
  }
})();

 

 

异步获取dom数目

const {Builder, By, Key, until, Options} = require("selenium-webdriver");
const fs = require("fs");
const firefox = require("selenium-webdriver/firefox");


(async function example() {
  let options = new firefox.Options()
  options.headless()
  let driver = await new Builder().forBrowser("firefox")
    .setFirefoxOptions(options)
    .build(options);
  try {
    await driver.get("http://www.baidu.com");
    await driver.findElement(By.id("kw")).sendKeys("webdriver", Key.RETURN);
    await driver.wait(until.titleIs("webdriver_百度搜索"), 5000)
    let sum = await driver.executeAsyncScript(
      `
function getSum(){
  let sum = 0
  function dfs(dom) {
    if (dom.children.length) {
        [...dom.children].forEach(dfs)
    }
    sum++
  };
  dfs(document)
  return sum
}
var callback = arguments[arguments.length - 1];

function init(){
  callback(getSum())
}
window.addEventListener("load", init, false);

`
    )
    console.log(sum)
  } finally {
    await driver.quit();
  }
})();