# -*- coding: utf-8 -*-from selenium import webdriverimport time, random, re, jsonfrom pynput.mouse import Button, Controller # 导入鼠标模块from pynput.keyboard import Controller as KeyController # 导入键盘模块from pynput.keyboard import Key # 导入键盘模块def run():user_list = [{'user': 'joyo', 'password': 'j123456'}]url = 'https://login.jhtml?'# ------------------------------------- ↓ 👇 设置 浏览器 👇 ↓ ------------------------------------------chromeOptions = webdriver.ChromeOptions() # 创建设置对象chromeOptions.add_experimental_option('excludeSwitches',['enable-automation']) # 设置webdriver为undefind, 因为自动驱动时该值为truechromeOptions.add_argument('--headless') # 浏览器无窗口加载chromeOptions.add_argument('--disable-gpu') # 不开启GPU加速chromeOptions.add_argument('--disable-dev-shm-usage') # 禁止chromeOptions.add_argument('--no-sandbox') # 以根用户打身份运行Chrome,使用-no-sandbox标记# chromeOptions.add_argument("--proxy-server = %s" % proxies['http']) # 设置代理# chromeOptions.add_argument(ua) # 设置ua 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',# chromeOptions.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面# chromeOptions.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度driver = webdriver.Chrome(chrome_options=chromeOptions, executable_path="/usr/bin/chromedriver") # linux 上加载设置driver = webdriver.Chrome(chrome_options=chromeOptions)# windows 上加载设置# -------------------------------------↑ 👆 设置 浏览器 👆 ↑ ------------------------------------------# driver = webdriver.Firefox() # 火狐浏览器driver = webdriver.Chrome() # 谷歌浏览器time.sleep(random.uniform(1, 3))driver.get(url) # 请求网页# ------------------------------------- ↓ 👇 设置 浏览器 窗口大小 👇 ↓ ------------------------------------------driver.maximize_window() # 窗口最大化# 将窗口大小实例化,打印浏览器的宽和高size_Dict = driver.get_window_size()print("当前浏览器的宽:", size_Dict['width'])print("当前浏览器的高:", size_Dict['height'])# 设置浏览器的大小driver.set_window_size(width=500, height=500, windowHandle="current") # windowHandle = "current" 控制当前窗口的意思, 不写的话默认控制当前窗口有# -------------------------------------↑ 👆 设置 浏览器 窗口大小 👆 ↑ ------------------------------------------# 读鼠标坐标mouse = Controller() # 创建 鼠标 对象# 设置鼠标坐标mouse.position = (1165, 320) # 设置鼠标位置# 点击鼠标mouse.click(Button.left) # 点击鼠标2下 mouse.click(Button.left, 2)time.sleep(0.5)mouse.release(Button.left) # 释放鼠标左键time.sleep(random.uniform(1, 2))k = KeyController() # 创建 键盘 对象# 清空输入框for i in range(18):k.press(Key.backspace) # 用键盘按住 backspacek.release(Key.backspace) # 放开backspacemouse.position = (1038, 420) # 设置鼠标位置mouse.click(Button.left) # 点击鼠标2下 mouse.click(Button.left, 2)time.sleep(0.5)mouse.release(Button.left) # 释放鼠标左键time.sleep(1)user = random.choice(user_list)# 输入用户名for i in user['user']: # 用键盘输入账号,一个字母一个字母的输入k.type(i)time.sleep(random.uniform(0.5, 1.5))k.press(Key.tab) # 用键盘按住 tab 键k.release(Key.tab) # 释放 tab 键 tab键可以切换输入框time.sleep(1)# 输入密码for i in user['password']:k.type(i)time.sleep(random.uniform(0.5, 1.5))# 鼠标移动到登录按钮位置mouse.position = (1030, 540)time.sleep(random.uniform(0.5, 1.5))mouse.click(Button.left) # 点击鼠标左键time.sleep(2)time.sleep(30)# driver.get("/quickview/shopkpi.shtml")# time.sleep(3)driver.get("https://shopkpi.shtml?")print('>>> ', driver.page_source, ' <<<')if __name__ == "__main__":run()