python selenium模拟点击的过程及实现是怎样
Admin 2022-09-08 群英技术资讯 280 次浏览
#下载包 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb #安装包,用deb方式安装 sudo dpkg -i --force-depends google-chrome-stable_current_amd64.deb #######################分割############################### #若出现Packet xxx is not installed,代表依赖出现问题执行以下命令 apt-get install -f #重新执行一遍 sudo dpkg -i --force-depends google-chrome-stable_current_amd64.deb #######################分割############################## #为了让其能够无界面运行 sudo apt-get install xvfb
#首先获取chromedriver的最新版本信息 LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE) #下载 wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip #解压 unzip chromedriver_linux64.zip #如果没有安装unzip就运行 apt install unzip #权限配置 chmod +x chromedriver #移动到bin目录下 sudo mv chromedriver /usr/bin/
#看自己系统是不是pip3,-i表示用什么源下载 pip3 install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
首先创建test.py文件,之后执行python3 test.py
,查看效果
#coding=utf-8 from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_opt = Options() # 创建参数设置对象. chrome_opt.add_argument('--headless') # 无界面化. chrome_opt.add_argument('--disable-gpu') # 配合上面的无界面化. chrome_opt.add_argument('--window-size=1366,768') # 设置窗口大小, 窗口大小会有影响. chrome_opt.add_argument("--no-sandbox") #使用沙盒模式运行 # 创建Chrome对象并传入设置信息. browser = webdriver.Chrome(chrome_options=chrome_opt) url = "https://www.baidu.com/" browser.get(url) print(browser.page_source) browser.quit()
这里我先用浏览器插件的Selenium先点击好导出python文件进行修改。程序需要创建data.csv
文件,并在其中写入账号密码
# Generated by Selenium IDE import sys import time from selenium import webdriver from selenium.webdriver.common.by import By # 读取数据函数 userList = [] def readData(): with open("data.csv","r") as f: for lines in f.readlines(): data = lines.strip().split(",") userList.append(data) # 全局函数 def printLog(info): print('{0} {1}'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),info)) # 打卡类 class Test: # 初始化浏览器 def __init__(self): # 添加 Chrome 相关配置 options = webdriver.ChromeOptions() # 指定设备名称即可 options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone X'}) options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') # options.add_argument('--headless') self.driver = webdriver.Chrome(chrome_options=options) # => 打开浏览器时加入配置 printLog("浏览器初始化完成") # 正式打卡 def signIn(self,username,password): printLog("正在打开网页") self.driver.get("http://stu.zstu.edu.cn/webroot/decision/url/mobile?origin=53fd9573-139e-4e3b-9357-4d791849ad58#/login") # 睡眠控制速度 # 刷新页面 self.driver.refresh() time.sleep(3) self.driver.set_window_size(800, 824) self.driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").click() self.driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").send_keys(username) self.driver.find_element(By.XPATH, "//input[@type=\'password\']").click() self.driver.find_element(By.XPATH, "//input[@type=\'password\']").send_keys(password) time.sleep(3) self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(4)").click() time.sleep(4) printLog("登录成功") # 寻找打卡功能模块 self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(2)").click() time.sleep(2) printLog("开始打卡") self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_6 .css-901oao").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[34]/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.CSS_SELECTOR, "#col_4_row_6 > div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[2]/div/div[2]/div/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.CSS_SELECTOR, "#col_5_row_6 > div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[2]/div/div[6]/div/div/div").click() time.sleep(1) self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_7 .css-1cwyjr8").click() self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_7 .css-1cwyjr8").send_keys("浙江理工大学") printLog("打卡进行中") #选项,不知道为什么css定位不能用 self.driver.find_element(By.XPATH, "//div[10]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[12]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[13]/div/div/div/div/div/div/div[2]").click() self.driver.find_element(By.XPATH, "//div[17]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[18]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[19]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[20]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[21]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[23]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[25]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[26]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[28]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[30]/div/div/div/div/div[2]/div/div").click() time.sleep(1) #提交 self.driver.find_element(By.CSS_SELECTOR, "#col_1_row_39").click() time.sleep(10) printLog("本次打卡成功") def quit(self): self.driver.quit() printLog("浏览器退出") if __name__ == '__main__': test = Test() readData() for i in range(len(userList)): try: test.signIn(userList[i][0],userList[i][1]) except: printLog("打卡成功或者系统bug,正在进行下一个") continue test.quit() printLog("今日打卡任务完成") sys.exit()
#上传代码后,设置定时 crontab -e #打开后添加以下记录,代表每天3点执行 0 3 * * * /usr/bin/python3 /home/shawn/ezl/sign.py > /home/shawn/ezl/elog.log 2>&1 #重启 systemctl restart cron.service
上面部分为研究生打开,下面为本科生
# Generated by Selenium IDE import sys import time from selenium import webdriver from selenium.webdriver.common.by import By # 读取数据函数 userList = [] def readData(): with open("data.csv","r") as f: for lines in f.readlines(): data = lines.strip().split(",") userList.append(data) # 全局函数 def printLog(info): print('{0} {1}'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),info)) # 打卡类 class Test: # 初始化浏览器 def __init__(self): # 添加 Chrome 相关配置 options = webdriver.ChromeOptions() # 指定设备名称即可 options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone X'}) options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--headless') self.driver = webdriver.Chrome(chrome_options=options) # => 打开浏览器时加入配置 printLog("浏览器初始化完成") # 正式打卡 def signIn(self,username,password): printLog("正在打开网页") self.driver.get("http://stu.zstu.edu.cn/webroot/decision/url/mobile?origin=53fd9573-139e-4e3b-9357-4d791849ad58#/login") # 睡眠控制速度 # 刷新页面 self.driver.refresh() time.sleep(3) self.driver.set_window_size(800, 824) self.driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").click() self.driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").send_keys(username) self.driver.find_element(By.XPATH, "//input[@type=\'password\']").click() self.driver.find_element(By.XPATH, "//input[@type=\'password\']").send_keys(password) time.sleep(3) self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(4)").click() time.sleep(4) printLog("登录成功") # 寻找打卡功能模块 # self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(2)").click() self.driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/div/div/div/div[1]/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div[3]/div/div/div[5]/div/div/div[3]").click() time.sleep(5) printLog("开始打卡") # self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_6 .css-901oao").click() self.driver.find_element(By.XPATH, "//*[@id='col_1_row_11']/span").click() time.sleep(5) self.driver.find_element(By.XPATH, "//div[2]/div/div/div/div/div/div/div/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[34]/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[2]/div/div/div/div/div/div/div/div/div/div[2]/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[2]/div/div[2]/div/div/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[3]/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//div[6]/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "//input").click() time.sleep(1) self.driver.find_element(By.XPATH, "//input").send_keys("浙江理工大学") time.sleep(1) printLog("打卡进行中") #选项,不知道为什么css定位不能用 self.driver.find_element(By.XPATH, "//div[11]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[13]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[14]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[18]/div/div/div/div/div/div/div[2]").click() self.driver.find_element(By.XPATH, "//div[19]/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[19]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[20]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[21]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[22]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[24]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[26]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "//div[27]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[29]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "//div[31]/div/div/div/div/div[2]/div/div").click() time.sleep(1) #提交 self.driver.find_element(By.XPATH, "//*[@id='col_0_row_40']/div/div/div/div").click() # self.driver.find_element(By.CSS_SELECTOR, "#col_0_row_40 .r-1loqt21 > .css-1dbjc4n").click() time.sleep(10) printLog("本次打卡成功") def quit(self): self.driver.quit() printLog("浏览器退出") if __name__ == '__main__': test = Test() readData() for i in range(len(userList)): try: test.signIn(userList[i][0],userList[i][1]) except: printLog("打卡成功或者系统bug,正在进行下一个") continue test.quit() printLog("今日打卡任务完成") sys.exit()
=已经失效,仅做参考=
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Python字符串关键点有下面几点:1 一些引号分隔的字符你可以把字符串看出是Python的一种数据类型,在Python单引号或者双引号之间的字符数组
大家好,本篇文章主要讲的是Python的函数使用详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
这篇文章主要为大家详细介绍了基于python实现双向链表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Python内置函数,chr() 用一个整数作参数,返回一个对应的字符。
本篇文章给大家带来了关于Python的相关知识,其中主要整理了自动化脚本的相关问题,包括了自动化阅读网页新闻、自动化数据探索、自动发送多封邮件等等内容,下面一起来看一下,希望对大家有帮助。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008