当前位置:   article > 正文

selenium商品自动发货_影刀 selenium

影刀 selenium

使用selenium对完成满足一定条件的商品进行自动发货

总体效果预览

selenium

导入需要的库

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. import time
  4. import pyautogui as ui
初始化webdriver
driver = webdriver.Chrome()
窗口最大化
driver.maximize_window()
初始化一个网页(影刀商城) 用法:driver.get('网页的url')
driver.get('https://demo-shop.winrobot360.com/user/login?redirect=%2Flist%2Ftable-list')
隐式等待
driver.implicitly_wait(5)

通过id定位到用户名和密码框,并输入账号密码,函数用法:

find_element(By.ID, 'id属性的名称')

send_keys('要输入的内容')

  1. #定位元素,找到用户名输入框
  2. user_name = driver.find_element(By.ID, 'username')
  3. #输入admin
  4. user_name.send_keys('admin')
  5. #定位元素,找到密码输入框
  6. pwd = driver.find_element(By.ID, 'password')
  7. pwd.send_keys('admin')

通过XPATH定位到登录按钮,并点击,函数用法:find_element(By.XPATH, 'XPATH路径')

  1. #定位登录按钮
  2. login = driver.find_element(By.XPATH, '//*[@id="formLogin"]/div[3]/div/div/span/button')
  3. #点击登录
  4. login.click()
跳转到网页,点击订单
  1. menu = driver.find_element(By.XPATH, "/html/body/div[1]/section/aside/div/ul/li[3]/a")
  2. menu.click()
找到存放页数的ul,找到总页数page_sum
  1. ul = driver.find_element(By.XPATH, '//*[@id="app"]/section/section/main/div/div/div/div[2]/div/div/div/div[3]/div/div/ul')
  2. ul_elements = ul.find_elements(By.TAG_NAME,'li')
  3. page = ul_elements[-3]
  4. page_sum = page.text

开始批量发货,最外层循环次数为刚才找到的总页数

  1. for i in range(int(page_sum)):
  2. print(i)
  3. #每一页开始操作之前把滚动条置于最上方
  4. js = "var q=document.documentElement.scrollTop=0"
  5. driver.execute_script(js)
  6. time.sleep(2)
  7. trs = driver.find_elements(By.CLASS_NAME,"ant-table-row.ant-table-row-level-0")
  8. print(trs)
  9. for tr in trs:
  10. tds = tr.find_elements(By.TAG_NAME,'td')
  11. print(tds)
  12. if tds[-2].text == '完成':
  13. #点击完成
  14. tds[-1].click()
  15. #等待一下确认按钮,点击确定
  16. time.sleep(0.5)
  17. driver.find_element(By.CLASS_NAME,'ant-btn.ant-btn-primary.ant-btn-sm').click()
  18. #按下箭头
  19. ui.press('down')
  20. ui.press('down')
  21. #点击下一页
  22. ul_elements[-2].click()

总体的代码

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. import time
  4. import pyautogui as ui
  5. #初始化webdriver
  6. driver = webdriver.Chrome()
  7. #窗口最大化
  8. driver.maximize_window()
  9. #初始化一个网页
  10. driver.get('https://demo-shop.winrobot360.com/user/login?redirect=%2Flist%2Ftable-list')
  11. #隐式等待
  12. driver.implicitly_wait(5)
  13. #定位元素,找到用户名输入框
  14. user_name = driver.find_element(By.ID, 'username')
  15. #输入admin
  16. user_name.send_keys('admin')
  17. #定位元素,找到密码输入框
  18. pwd = driver.find_element(By.ID, 'password')
  19. pwd.send_keys('admin')
  20. #定位登录按钮
  21. login = driver.find_element(By.XPATH, '//*[@id="formLogin"]/div[3]/div/div/span/button')
  22. #点击登录
  23. login.click()
  24. #页面发生跳转,进入一个新的页面
  25. time.sleep(1)
  26. #点击订单
  27. menu = driver.find_element(By.XPATH, "/html/body/div[1]/section/aside/div/ul/li[3]/a")
  28. menu.click()
  29. time.sleep(2)
  30. #找到存放页数的ul,找到总页数page_sum
  31. ul = driver.find_element(By.XPATH, '//*[@id="app"]/section/section/main/div/div/div/div[2]/div/div/div/div[3]/div/div/ul')
  32. ul_elements = ul.find_elements(By.TAG_NAME,'li')
  33. page = ul_elements[-3]
  34. page_sum = page.text
  35. print(page)
  36. for i in range(int(page_sum)):
  37. print(i)
  38. #每一页开始操作之前把滚动条置于最上方
  39. js = "var q=document.documentElement.scrollTop=0"
  40. driver.execute_script(js)
  41. time.sleep(2)
  42. trs = driver.find_elements(By.CLASS_NAME,"ant-table-row.ant-table-row-level-0")
  43. print(trs)
  44. for tr in trs:
  45. tds = tr.find_elements(By.TAG_NAME,'td')
  46. print(tds)
  47. if tds[-2].text == '完成':
  48. #点击完成
  49. tds[-1].click()
  50. #等待一下确认按钮,点击确定
  51. time.sleep(0.5)
  52. driver.find_element(By.CLASS_NAME,'ant-btn.ant-btn-primary.ant-btn-sm').click()
  53. #按下箭头
  54. ui.press('down')
  55. ui.press('down')
  56. #点击下一页
  57. ul_elements[-2].click()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/282006
推荐阅读
相关标签
  

闽ICP备14008679号