page-init.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // 保存文件
  2. const fs = require('fs');
  3. const path = require('path');
  4. const webdriver = require('selenium-webdriver');
  5. const { saveFile } = require('../tools/save-file');
  6. const { baseData } = require('../tools/base-data');
  7. const { skuData } = require('../tools/sku-data');
  8. const { downImg } = require('./down-img');
  9. /**
  10. * 详情页初始化准备
  11. * @param { WebDriver } driver
  12. */
  13. function pageInit(driver) {
  14. return new Promise(async (resolve, reject) => {
  15. // 自动滚动 到 y=20000
  16. driver.executeScript(function () {
  17. let y = 0;
  18. let timer = setInterval(() => {
  19. y += 50;
  20. if (y > 20000) {
  21. clearInterval(timer);
  22. }
  23. window.scrollTo(0, y)
  24. }, 10)
  25. })
  26. console.log('自动滚动完成');
  27. // 检查基本信息(判断详情加载完成)
  28. await driver.sleep(20000 / 50 * 10 + 1000)
  29. // 判断是否有验证框
  30. try {
  31. const dialog = await driver.findElement(webdriver.By.className('baxia-dialog'));
  32. if (dialog) {
  33. const ele = await driver.findElement(webdriver.By.js(() => document.querySelector('.baxia-dialog .baxia-dialog-close')))
  34. ele.click();
  35. }
  36. } catch (error) {
  37. }
  38. let data = {};
  39. // 基础详情
  40. const bData = await baseData(driver);
  41. data = { ...bData };
  42. console.log('基础数据获取完成')
  43. // sku
  44. const sku = await skuData(driver);
  45. data.sku = sku;
  46. console.log('sku获取完成')
  47. // 保存文件
  48. const configObj = global.configObj;
  49. let path = configObj.cateName + '/';
  50. data.cate = configObj.cateName;
  51. if (configObj.cate2Exist) {
  52. path += configObj.cate2Name + '/';
  53. data.cate2 = configObj.cate2Name;
  54. }
  55. saveFile(`${data.title}.json`, JSON.stringify(data, null, 4), path);
  56. // 下载该文件图片
  57. downImg(data);
  58. // 3~8s 后关闭
  59. const random = parseInt(Math.random() * 5 + 3) * 1000;
  60. setTimeout(() => {
  61. console.log('完成一个');
  62. resolve();
  63. }, random)
  64. })
  65. }
  66. exports.pageInit = pageInit;