1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- // 保存文件
- const fs = require('fs');
- const path = require('path');
- const webdriver = require('selenium-webdriver');
- const { saveFile } = require('../tools/save-file');
- const { baseData } = require('../tools/base-data');
- const { skuData } = require('../tools/sku-data');
- const { downImg } = require('./down-img');
- /**
- * 详情页初始化准备
- * @param { WebDriver } driver
- */
- function pageInit(driver) {
- return new Promise(async (resolve, reject) => {
- // 自动滚动 到 y=20000
- driver.executeScript(function () {
- let y = 0;
- let timer = setInterval(() => {
- y += 50;
- if (y > 20000) {
- clearInterval(timer);
- }
- window.scrollTo(0, y)
- }, 10)
- })
- console.log('自动滚动完成');
- // 检查基本信息(判断详情加载完成)
- await driver.sleep(20000 / 50 * 10 + 1000)
- // 判断是否有验证框
- try {
- const dialog = await driver.findElement(webdriver.By.className('baxia-dialog'));
- if (dialog) {
- const ele = await driver.findElement(webdriver.By.js(() => document.querySelector('.baxia-dialog .baxia-dialog-close')))
- ele.click();
- }
- } catch (error) {
- }
- let data = {};
- // 基础详情
- const bData = await baseData(driver);
- data = { ...bData };
- console.log('基础数据获取完成')
- // sku
- const sku = await skuData(driver);
- data.sku = sku;
- console.log('sku获取完成')
- // 保存文件
- const configObj = global.configObj;
- let path = configObj.cateName + '/';
- data.cate = configObj.cateName;
- if (configObj.cate2Exist) {
- path += configObj.cate2Name + '/';
- data.cate2 = configObj.cate2Name;
- }
-
- saveFile(`${data.title}.json`, JSON.stringify(data, null, 4), path);
- // 下载该文件图片
- downImg(data);
- // 3~8s 后关闭
- const random = parseInt(Math.random() * 5 + 3) * 1000;
- setTimeout(() => {
- console.log('完成一个');
- resolve();
- }, random)
- })
- }
- exports.pageInit = pageInit;
|