123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- const webdriver = require('selenium-webdriver');
- const chrome = require('selenium-webdriver/chrome');
- const firefox = require('selenium-webdriver/firefox');
- const { eachList } = require('./tools/each-list');
- (async function example() {
- // let driver = await new Builder().forBrowser('chrome').build();
- let driver = new webdriver.Builder()
- .forBrowser('chrome')
- .build();
- driver.get('https://lumn.tmall.com/search.htm?spm=a1z10.3-b-s.w4011-15438998729.1.31ba3b51KXJsTQ');
- let configObj = {
- originalWindow: '',
- cateName: '',
- cateWindow: '',
- cateIndex: 0,
- cateListNum: 0,
- cate2Exist: false,
- cate2Name: '',
- catewWindow: '',
- cate2Index: 0,
- cate2ListNum: 0,
- }
- global.configObj = configObj;
- // 检查登录
- await driver.wait(webdriver.until.elementLocated(webdriver.By.className('j_Username')), 100000, '超时未登录');
- console.log('登陆成功');
- // 存储原始窗口的 ID
- configObj.originalWindow = await driver.getWindowHandle();
- await driver.sleep(1500)
- // 获取分类列表
- const cateList = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('.cateAttrs li a')));
- configObj.cateIndex = 0;
- configObj.cateListNum = cateList.length;
- // 确定当前分类
- const curCate = cateList[configObj.cateIndex];
- configObj.cateName = await curCate.getText();
- const url = await curCate.getAttribute('href');
- console.log(url)
- // 切换新窗口打开分类
- await driver.switchTo().newWindow('tab');
- configObj.cateWindow = await driver.getWindowHandle();
- await driver.get(url);
- await driver.sleep(1500)
- // 判断是否有二级分类
- const cate2List = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('.cateAttrs li a')));
- if (cate2List.length) {
- configObj.cate2Exist = true;
- configObj.cate2Index = 0;
- configObj.cateListNum = cate2List.length;
- // 确定当前分类
- const curCate2 = cate2List[configObj.cate2Index];
- configObj.cate2Name = await curCate2.getText();
- const url2 = await curCate2.getAttribute('href');
- // 切换新窗口打开分类
- await driver.switchTo().newWindow('tab');
- configObj.cate2Window = await driver.getWindowHandle();
- await driver.get(url2);
- await driver.sleep(1500)
- } else {
- configObj.cate2Exist = false;
- }
- await eachList(driver);
- })();
|