app-list.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.w4011-15438998729.1.31ba3b51KXJsTQ');
  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. const curCate = cateList[configObj.cateIndex];
  36. configObj.cateName = await curCate.getText();
  37. const url = await curCate.getAttribute('href');
  38. console.log(url)
  39. // 切换新窗口打开分类
  40. await driver.switchTo().newWindow('tab');
  41. configObj.cateWindow = await driver.getWindowHandle();
  42. await driver.get(url);
  43. await driver.sleep(1500)
  44. // 判断是否有二级分类
  45. const cate2List = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('.cateAttrs li a')));
  46. if (cate2List.length) {
  47. configObj.cate2Exist = true;
  48. configObj.cate2Index = 0;
  49. configObj.cateListNum = cate2List.length;
  50. // 确定当前分类
  51. const curCate2 = cate2List[configObj.cate2Index];
  52. configObj.cate2Name = await curCate2.getText();
  53. const url2 = await curCate2.getAttribute('href');
  54. // 切换新窗口打开分类
  55. await driver.switchTo().newWindow('tab');
  56. configObj.cate2Window = await driver.getWindowHandle();
  57. await driver.get(url2);
  58. await driver.sleep(1500)
  59. } else {
  60. configObj.cate2Exist = false;
  61. }
  62. await eachList(driver);
  63. })();