123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- 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.w5002-15438998724.1.248c3b51L3CXmJ&search=y');
- 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;
- // 循环分类
- async function eachCate() {
- // 确定当前分类
- const curCate = cateList[configObj.cateIndex];
- configObj.cateName = await curCate.getText();
- const url = await curCate.getAttribute('href');
- // 切换新窗口打开分类 一级分类
- 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.cate2ListNum = cate2List.length;
- function eachCate2() {
- return new Promise(async (resolve, reject) => {
- // 确定当前分类
- 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);
- await eachList(driver);
- configObj.cate2Index++;
- //判断耳机分类是否循环完
- if (configObj.cate2Index === configObj.cate2ListNum) {
- configObj.cate2Exist = false;
- configObj.cate2Name = '';
- configObj.catewWindow = '';
- configObj.cate2Index = 0;
- configObj.cate2ListNum = 0;
- await driver.close();
- await driver.switchTo().window(configObj.cateWindow);
- resolve();
- } else {
- eachCate2();
- }
- });
- }
- await eachCate2();
- } else {
- configObj.cate2Exist = false;
- await eachList(driver);
- }
- configObj.cateIndex++;
- if(configObj.cateIndex === configObj.cateListNum){
- console.log('一级分类完毕');
- }else{
- eachCate();
- }
- }
- eachCate();
- })();
|