#!/Users/xuyeqiang/opt/miniconda3/envs/f6/bin/python3.9 from playwright.sync_api import Playwright, sync_playwright import re import pandas as pd js = """ Object.defineProperties(navigator, {webdriver:{get:()=>undefined}}); """ def run(playwright: Playwright) -> None: browser = playwright.chromium.launch(headless=False) context = browser.new_context(viewport={'width': 1700, 'height': 1080}) # Open new page page = context.new_page() page.add_init_script(js) # 隐藏 webdriver属性,不然拖动滑块会失败。 # Go to https://fws.carzone365.com/#/store/quitAudit page.goto("https://fws.carzone365.com/#/store/quitAudit") # Click [placeholder="请输入用户名"] page.click("[placeholder=\"请输入用户名\"]") # Fill [placeholder="请输入用户名"] page.fill("[placeholder=\"请输入用户名\"]", "17710217084") # Click [placeholder="请输入密码"] page.click("[placeholder=\"请输入密码\"]") # Fill [placeholder="请输入密码"] page.fill("[placeholder=\"请输入密码\"]", "123456F6!") """ 拖拽滑块验证 """ deltaX = 50000 steps = 100 element = page.wait_for_selector("text=请按住滑块,拖动到最右边") boundingBox = element.bounding_box() if boundingBox: x = boundingBox.get('x') + boundingBox.get('width') / 2 y = boundingBox.get('y') + boundingBox.get('height') / 2 page.mouse.move(x, y) page.mouse.down() x1 = x + deltaX page.mouse.move(x1, y, steps=steps) page.mouse.up() page.wait_for_timeout(1000) page.click('xpath=//*[@id="app"]//button[contains(@class,"login-btn")]') # 登录 """ 开始自动化点击操作 """ page.click('xpath=//*[@id="app"]/section/section/aside/ul/li[2]/ul/li[2]/div/div') # 门店审批 # 将每一页显示的数量设置为100 page.click('xpath=//*[@id="app"]//input[@placeholder="请选择"]') page.click('xpath=//span[text()="100条/页"]') page.wait_for_timeout(2000) page.click('xpath=//*[@id="app"]/section/section/main/div/div[3]/div[2]/div[2]/button[2]/span') # 查询 page.wait_for_timeout(1000) # 查询出一共有多少条数据 input_string = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[3]/div/span[1]') # 使用正则表达式提取数字部分 numbers = re.findall(r'\d+', input_string) # 将提取到的数字部分转换为整数列表 numbers = [int(num) for num in numbers][0] print(f'numbers:{numbers}') # 计算总页数 total_pages = (numbers + 100 - 1) // 100 # 计算最后一页条数 def calculate_last_page_data(total_numbers): data_per_page = 100 last_page_data = total_numbers % data_per_page return last_page_data if last_page_data != 0 else data_per_page last_page_data = calculate_last_page_data(numbers) print("最后一页显示的数据条数:", last_page_data) # 如果需要翻页,可以在这里添加翻页的逻辑 # 创建一个空列表来存储每行的数据 data = [] last_page_data_len = 100 for page_new in range(1, total_pages + 1): print(f"处理第 {page_new} 页的数据") if page_new == total_pages:last_page_data_len = last_page_data for i in range(1,last_page_data_len + 1): # 逐条获取明细 string_1 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[1]/div') string_2 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[2]/div') string_3 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[3]/div') string_4 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[4]/div') string_5 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[5]/div') string_6 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[6]/div') string_7 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[7]/div') string_8 = page.text_content('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[2]/div[3]/table/tbody/tr['+str(i)+']/td[8]/div') # 将数据添加到列表中 data.append([string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8]) if page_new != total_pages: try: page.click('xpath=//*[@id="app"]/section/section/main/div/div[4]/div[3]/div/button[2]/i') # 下一页 page.wait_for_timeout(1000) except: pass # 创建DataFrame df = pd.DataFrame(data, columns=["类型", "门店名称", "门店id", "门店地址", "分类", "申请人", "状态", "申请时间"]) df.to_excel(r"C:\Users\admin\Desktop\门店审批明细.xlsx") page.wait_for_timeout(1000) # --------------------- context.close() browser.close() with sync_playwright() as playwright: run(playwright)