app-cate.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const webdriver = require('selenium-webdriver');
  2. const chrome = require('selenium-webdriver/chrome');
  3. const firefox = require('selenium-webdriver/firefox');
  4. const { eachList } = require('./tools/each-list');
  5. (async function example() {
  6. // let driver = await new Builder().forBrowser('chrome').build();
  7. let driver = new webdriver.Builder()
  8. .forBrowser('chrome')
  9. .build();
  10. driver.get('https://lumn.tmall.com/search.htm?spm=a1z10.3-b-s.w5002-15438998724.1.248c3b51L3CXmJ&search=y');
  11. let configObj = {
  12. originalWindow: '',
  13. cateName: '',
  14. cateWindow: '',
  15. cateIndex: 0,
  16. cateListNum: 0,
  17. cate2Exist: false,
  18. cate2Name: '',
  19. catewWindow: '',
  20. cate2Index: 0,
  21. cate2ListNum: 0,
  22. }
  23. global.configObj = configObj;
  24. // 检查登录
  25. await driver.wait(webdriver.until.elementLocated(webdriver.By.className('j_Username')), 100000, '超时未登录');
  26. console.log('登陆成功');
  27. // 存储原始窗口的 ID
  28. configObj.originalWindow = await driver.getWindowHandle();
  29. await driver.sleep(1500)
  30. // 获取分类列表
  31. const cateList = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('.cateAttrs li a')));
  32. configObj.cateIndex = 0;
  33. configObj.cateListNum = cateList.length;
  34. // 循环分类
  35. async function eachCate() {
  36. // 确定当前分类
  37. const curCate = cateList[configObj.cateIndex];
  38. configObj.cateName = await curCate.getText();
  39. const url = await curCate.getAttribute('href');
  40. // 切换新窗口打开分类 一级分类
  41. await driver.switchTo().newWindow('tab');
  42. configObj.cateWindow = await driver.getWindowHandle();
  43. await driver.get(url);
  44. await driver.sleep(1500)
  45. // 判断是否有二级分类
  46. const cate2List = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('.cateAttrs li a')));
  47. if (cate2List.length) {
  48. configObj.cate2Exist = true;
  49. configObj.cate2Index = 0;
  50. configObj.cate2ListNum = cate2List.length;
  51. function eachCate2() {
  52. return new Promise(async (resolve, reject) => {
  53. // 确定当前分类
  54. const curCate2 = cate2List[configObj.cate2Index];
  55. configObj.cate2Name = await curCate2.getText();
  56. const url2 = await curCate2.getAttribute('href');
  57. // 切换新窗口打开 二级分类
  58. await driver.switchTo().newWindow('tab');
  59. configObj.cate2Window = await driver.getWindowHandle();
  60. await driver.get(url2);
  61. await driver.sleep(1500);
  62. await eachList(driver);
  63. configObj.cate2Index++;
  64. //判断耳机分类是否循环完
  65. if (configObj.cate2Index === configObj.cate2ListNum) {
  66. configObj.cate2Exist = false;
  67. configObj.cate2Name = '';
  68. configObj.catewWindow = '';
  69. configObj.cate2Index = 0;
  70. configObj.cate2ListNum = 0;
  71. await driver.close();
  72. await driver.switchTo().window(configObj.cateWindow);
  73. resolve();
  74. } else {
  75. eachCate2();
  76. }
  77. });
  78. }
  79. await eachCate2();
  80. } else {
  81. configObj.cate2Exist = false;
  82. await eachList(driver);
  83. }
  84. configObj.cateIndex++;
  85. if(configObj.cateIndex === configObj.cateListNum){
  86. console.log('一级分类完毕');
  87. }else{
  88. eachCate();
  89. }
  90. }
  91. eachCate();
  92. })();