赞
踩
使用selenium对完成满足一定条件的商品进行自动发货
总体效果预览
selenium
导入需要的库
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- import time
- 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('要输入的内容')
- #定位元素,找到用户名输入框
- user_name = driver.find_element(By.ID, 'username')
-
- #输入admin
- user_name.send_keys('admin')
-
- #定位元素,找到密码输入框
- pwd = driver.find_element(By.ID, 'password')
- pwd.send_keys('admin')
通过XPATH定位到登录按钮,并点击,函数用法:find_element(By.XPATH, 'XPATH路径')
- #定位登录按钮
- login = driver.find_element(By.XPATH, '//*[@id="formLogin"]/div[3]/div/div/span/button')
- #点击登录
- login.click()
跳转到网页,点击订单
- menu = driver.find_element(By.XPATH, "/html/body/div[1]/section/aside/div/ul/li[3]/a")
- menu.click()
找到存放页数的ul,找到总页数page_sum
- ul = driver.find_element(By.XPATH, '//*[@id="app"]/section/section/main/div/div/div/div[2]/div/div/div/div[3]/div/div/ul')
- ul_elements = ul.find_elements(By.TAG_NAME,'li')
- page = ul_elements[-3]
- page_sum = page.text
开始批量发货,最外层循环次数为刚才找到的总页数
- for i in range(int(page_sum)):
- print(i)
- #每一页开始操作之前把滚动条置于最上方
- js = "var q=document.documentElement.scrollTop=0"
- driver.execute_script(js)
- time.sleep(2)
- trs = driver.find_elements(By.CLASS_NAME,"ant-table-row.ant-table-row-level-0")
- print(trs)
- for tr in trs:
- tds = tr.find_elements(By.TAG_NAME,'td')
- print(tds)
- if tds[-2].text == '完成':
- #点击完成
- tds[-1].click()
- #等待一下确认按钮,点击确定
- time.sleep(0.5)
- driver.find_element(By.CLASS_NAME,'ant-btn.ant-btn-primary.ant-btn-sm').click()
- #按下箭头
- ui.press('down')
- ui.press('down')
-
- #点击下一页
- ul_elements[-2].click()
总体的代码
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- import time
- import pyautogui as ui
-
- #初始化webdriver
- driver = webdriver.Chrome()
-
- #窗口最大化
- driver.maximize_window()
-
- #初始化一个网页
- driver.get('https://demo-shop.winrobot360.com/user/login?redirect=%2Flist%2Ftable-list')
-
- #隐式等待
- driver.implicitly_wait(5)
-
- #定位元素,找到用户名输入框
- user_name = driver.find_element(By.ID, 'username')
-
- #输入admin
- user_name.send_keys('admin')
-
- #定位元素,找到密码输入框
- pwd = driver.find_element(By.ID, 'password')
- pwd.send_keys('admin')
-
- #定位登录按钮
- login = driver.find_element(By.XPATH, '//*[@id="formLogin"]/div[3]/div/div/span/button')
- #点击登录
- login.click()
-
- #页面发生跳转,进入一个新的页面
- time.sleep(1)
- #点击订单
- menu = driver.find_element(By.XPATH, "/html/body/div[1]/section/aside/div/ul/li[3]/a")
- menu.click()
- time.sleep(2)
-
- #找到存放页数的ul,找到总页数page_sum
- ul = driver.find_element(By.XPATH, '//*[@id="app"]/section/section/main/div/div/div/div[2]/div/div/div/div[3]/div/div/ul')
- ul_elements = ul.find_elements(By.TAG_NAME,'li')
- page = ul_elements[-3]
- page_sum = page.text
- print(page)
- for i in range(int(page_sum)):
- print(i)
- #每一页开始操作之前把滚动条置于最上方
- js = "var q=document.documentElement.scrollTop=0"
- driver.execute_script(js)
- time.sleep(2)
- trs = driver.find_elements(By.CLASS_NAME,"ant-table-row.ant-table-row-level-0")
- print(trs)
- for tr in trs:
- tds = tr.find_elements(By.TAG_NAME,'td')
- print(tds)
- if tds[-2].text == '完成':
- #点击完成
- tds[-1].click()
- #等待一下确认按钮,点击确定
- time.sleep(0.5)
- driver.find_element(By.CLASS_NAME,'ant-btn.ant-btn-primary.ant-btn-sm').click()
- #按下箭头
- ui.press('down')
- ui.press('down')
-
-
- #点击下一页
- ul_elements[-2].click()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。