page-init.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. function pageInit(driver) {
  10. return new Promise((resolve, reject) => {
  11. driver.executeScript(function () {
  12. let y = 0;
  13. let timer = setInterval(() => {
  14. y += 50;
  15. if (y > 20000) {
  16. clearInterval(timer);
  17. }
  18. window.scrollTo(0, y)
  19. }, 10)
  20. })
  21. // 检查基本信息(判断详情加载完成)
  22. driver.sleep(20000 / 50 * 10 + 1000).then(async () => {
  23. console.log('自动滚动完成');
  24. // 判断是否有验证框
  25. try {
  26. const dialog = await driver.findElement(webdriver.By.className('baxia-dialog'));
  27. if (dialog) {
  28. const ele = await driver.findElement(webdriver.By.js(() => document.querySelector('.baxia-dialog .baxia-dialog-close')))
  29. ele.click();
  30. }
  31. console.log('有验证框');
  32. } catch (error) {
  33. }
  34. let data = {};
  35. // 获取基础详情
  36. baseData(driver).then(bData => {
  37. data = { ...bData };
  38. console.log('基础数据获取完成')
  39. skuData(driver).then(sku => {
  40. data.sku = sku;
  41. console.log('sku获取完成')
  42. saveFile(`/json/${data.title}.json`, JSON.stringify(data, null, 4));
  43. downImg(data);
  44. const random = parseInt(Math.random() * 6 + 3) * 1000;
  45. setTimeout(() => {
  46. console.log('完成一个');
  47. resolve();
  48. }, random)
  49. })
  50. });
  51. })
  52. });
  53. }
  54. exports.pageInit = pageInit;