123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- // 保存文件
- const fs = require('fs');
- const path = require('path');
- const webdriver = require('selenium-webdriver');
- const { pageInit } = require('./page-init');
- /**
- * 循环列表
- * @param { WebDriver } driver
- */
- function eachList(driver) {
- return new Promise(async (resolve, reject) => {
- // 等待页面加载
- await driver.wait(webdriver.until.elementLocated(webdriver.By.js(() => document.querySelectorAll('#J_ShopSearchResult .item4line1 .item'))));
- const index = 0;
- const listWindow = await driver.getWindowHandle();
- async function eachList(index) {
- let list = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('#J_ShopSearchResult .item4line1 .item')));
- // 手动减去推荐(暂无其他办法)
- const endNum = list.length - 8;
- console.log(['获取list ok', endNum])
- const listItem = await list[index].findElement(webdriver.By.tagName('a'))
- const url = await listItem.getAttribute('href');
- // 等待新窗口或标签页
- await driver.switchTo().newWindow('tab');
- await driver.get(url);
- // 等待新标签页完成加载内容
- await driver.sleep(2000);
- await pageInit(driver);
- index++;
- if (index === endNum) {
- console.log('一个分类结束')
- resolve();
- } else {
- console.log(['\n\n', '开始', index + 1, '---------------------------------------------------------------------'])
- await driver.close();
- await driver.switchTo().window(listWindow);
- eachList(index);
- }
- }
- eachList(index);
- });
- }
- exports.eachList = eachList;
|