node+selenium-webdriver实现自动化测试

684 阅读2分钟

中文文档:www.selenium.dev/zh-cn/docum…

一 、 安装类库

npm install selenium-webdriver

二 、 安装浏览器驱动、并配置到环境变量

中文文档:

www.selenium.dev/zh-cn/docum…

三、编写第一个Selenium脚本

1. 使用驱动实例开启会话

driver = await new Builder().forBrowser('chrome').build();

2. 在浏览器上执行操作

导航到一个网页

await driver.get('https://www.baidu.com')

3. 请求浏览器信息

let title = await driver.getTitle();

4. 建立等待策略

在尝试定位元素之前, 确保该元素位于页面上, 并且在尝试与该元素交互之前, 该元素处于可交互状态. 隐式等待很少是最好的解决方案

await driver.manage().setTimeouts({ implicit: 500 });

5. 发送命查找元素

let textBox = await driver.findElement(By.name('my-text'));
let submitButton = await driver.findElement(By.css('button'));

6. 操作元素

await textBox.sendKeys('Selenium');
await submitButton.click();

7. 获取元素信息

let value = await message.getText();

ps:selenium只会与可见元素交互,所以获取隐藏元素的文本总是会返回空字符串。

let value = await message.getAttribute("innerText");

8. 结束会话

这将结束驱动程序进程, 默认情况下, 该进程也会关闭浏览器. 无法向此驱动程序实例发送更多命令.

after(async () => await driver.quit());

例子


const { Builder, By } = require("selenium-webdriver");
const fs = require("fs");

const { spliderUrl } = require("../static/constatnce");

//获取主页面并处理数据
async function getMainHtml() {
  let mainHtml = {
    icons: {},
    shoppClassifyInfo: [],
  };

  //1.使用驱动开启一个会话
  let driver = new Builder().forBrowser("chrome").build();
  await driver.get(spliderUrl);
  console.log(`${new Date()}   正在打开浏览器~`);

  //2.分析页面,处理数据
  console.log(`${new Date()}   开始分析主页面数据~`);
  //2.1获取顶部左上角图片信息
  let topLogoUrl = await driver
    .findElement(By.css(".hd-logo>img"))
    .getAttribute("src");

  mainHtml.icons.topLogoUrl = topLogoUrl;

  //2.2获取首页国家信息
  let countrys = await driver.findElements(By.css(".nav-country>ul>li>a"));
  let countryInfo = [];
  for (let i of countrys) {
    countryInfo.push(await i.getText());
  }
  mainHtml.icons.countryInfo = countryInfo;

  //2.3获取商品列表
  let firstClassify = await driver.findElements(By.css(".nav-classify-item")); //1级商品列表
  let shoppClassifyInfo = [];
  //2.3.1一级列表信息获取
  for (let i of firstClassify) {
    let firstClassifyInfo = {
      title: "",
      iconUrl: null,
      children: [],
    };
    firstClassifyInfo.title = await i
      .findElement(By.css(".nav-classify-title"))
      .getText();

    //2.3.2二级菜单信息获取
    let secClassify = await i.findElements(By.css(".nav-classify-list-item"));
    for (let j of secClassify) {
      let secClassifyInfo = {
        title: "",
        iconUrl: null,
      };
      secClassifyInfo.title = await j
        .findElement(By.css("span"))
        .getAttribute("innerText");
      secClassifyInfo.iconUrl = await j
        .findElement(By.css("img"))
        .getAttribute("data-image");
      console.log(secClassifyInfo);
      firstClassifyInfo.children.push(secClassifyInfo);
    }
    shoppClassifyInfo.push(firstClassifyInfo);
  }
  mainHtml.shoppClassifyInfo = shoppClassifyInfo;
  console.log(`${new Date()}   开始主页面数据分析完成~`);

  fs.writeFileSync("./public/mainHtml.json", JSON.stringify(mainHtml));
  await driver.quit();
}

module.exports = { getMainHtml };


更多API参考

后续更新...