python selenium模拟点击问题解决方案

目录
  • 1、安装谷歌浏览器
  • 2、安装浏览器驱动
  • 3、安装selenium
  • 4、简单测试
  • 5、打卡程序
  • 6、linux设置定时任务
  • 7、其他

1、安装谷歌浏览器

#下载包
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

2、安装浏览器驱动

#首先获取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/

3、安装selenium

#看自己系统是不是pip3,-i表示用什么源下载
pip3 install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple

4、简单测试

首先创建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()

5、打卡程序

这里我先用浏览器插件的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()

6、linux设置定时任务

#上传代码后,设置定时
crontab -e
#打开后添加以下记录,代表每天3点执行
0 3 * * * /usr/bin/python3 /home/shawn/ezl/sign.py > /home/shawn/ezl/elog.log 2>&1
#重启
systemctl restart cron.service 

7、其他

上面部分为研究生打开,下面为本科生

# 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()

=已经失效,仅做参考=

到此这篇关于python selenium模拟点击的文章就介绍到这了,更多相关python 模拟点击内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • python+selenium 定位到元素,无法点击的解决方法

    报错 selenium.common.exceptions.WebDriverException: Message: Element is not clickable at point (234.75, 22). Other element would receive the click: <img class="logo" src="/public/desktop/common/img/game_logo.png"> 需要点击的按钮页面显示不了,需要下

  • Python+selenium点击网页上指定坐标的实例

    例如有些页面元素很难获取,但是位置很固定,那么可以直接用坐标来进行操作 例如要对页面上的(x:200, y:100)进行操作,可以用如下代码: from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains dr = webdriver.Chrome() dr.get('http://www.baidu.com') ActionChains(dr).move_by_of

  • python+Selenium自动化测试——输入,点击操作

    这是我的第一个真正意思上的自动化脚本. 1.练习的测试用例为: 打开百度首页,搜索"胡歌",然后检索列表,有无"胡歌的新浪微博"这个链接 2.在写脚本之前,需要明确测试的步骤,具体到每个步骤需要做什么,既拆分测试场景,考虑好之后,再去写脚本. 此测试场景拆分如下: 1)启动Chrome浏览器 2)打开百度首页,https://www.baidu.com 3)定位搜索输入框,输入框元素XPath表达式://*[@id="kw"] 4)定位搜索提交按

  • python+selenium 点击单选框-radio的实现方法

    例子:以百度文库中选择文档的类型为例 问题一:遍历点击所有文档类型的单选框 # coding=utf-8 from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.maximize_window() driver.get("http://wenku.baidu.com") driver.implicitly_wait(8) for i in driver.find_e

  • Python selenium模拟网页点击爬虫交管12123违章数据

    在上一篇文章<Python教程-模拟网页点击爬虫定位系统>讲解怎么通过模拟点击方式爬取车辆定位数据,本次介绍怎么以模拟点击方式进入交管12123爬取车辆违章数据,本文直接讲解过程,使用的命令解释见上一篇文章.本文同<Python教程-模拟网页点击爬虫定位系统>同样为企业中实际的爬虫案例,如果之后想进入车企行业可以做个了解. 准备工具:spyder.selenium库.google浏览器及对应版本的chromedriver.exe 效果 注:分享此案例目的是为了帮助同行解放双手,更好

  • python selenium模拟点击问题解决方案

    目录 1.安装谷歌浏览器 2.安装浏览器驱动 3.安装selenium 4.简单测试 5.打卡程序 6.linux设置定时任务 7.其他 1.安装谷歌浏览器 #下载包 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 #####

  • Python selenium模拟手动操作实现无人值守刷积分功能

    经常为学校的各种刷分而发愁,得知开学无望,日后还要刷课,索性自动化一次,学而不用乃愚昧 聪慧 四大模块 初始化 from selenium import webdriver if __name__ == '__main__': driver = webdriver.Chrome() url = 'https://pc.xuexi.cn/points/login.html?ref=https://pc.xuexi.cn/points/my-points.html' driver.get(url =

  • python模拟点击网页按钮实现方法

    python怎么模拟点击网页按钮 前提环境: Python3 和 Visual Studio Code安装完毕 安装selenium : 在终端输入: pip install selenium, 如图 下载chromedriver : 登录http://chromedriver.storage.googleapis.com/index.html下载 (与浏览器对应版本请上网查阅) 下载后将chromedriver.exe文件放至 python.exe所在目录 如: D:\Program File

  • python中用ctypes模拟点击的实例讲解

    在小编学习python中的模拟点击之前,我们想要对某一项操作进行自动指令的重复,可以选择大家熟知的按键精灵.那么对比python的模拟点击,小编还是觉得python中使用更加方便.这样说不能让有些小伙伴信服,下面小编就以一个以小游戏为例,在我们写完ctypes模拟点击后用python运行,看看游戏体验效果. 按键精灵提供的窗口api性能并不算的上太好.但是将整个逻辑搬到python上,并提供了自己所写的api后,速度有了很大的提升. 直接用python调用,获取特定点位置上的颜色,非白色就发送点

  • python3.7+selenium模拟淘宝登录功能的实现

    在使用selenium去获取淘宝商品信息时会遇到登录界面 这个登录界面处理的难度在于滑动验证的实现,有的人使用微博登录,避免了滑动验证,那可不可以使用密码登录呢?答案是可以的 实现思路 首先导入需要的库 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.web

  • Python爬虫模拟登陆哔哩哔哩(bilibili)并突破点选验证码功能

    写在前面   今天带给大家一个突破点选验证码的案例,利用爬虫模拟登陆哔哩哔哩,并且把一些采坑的地方给大家强调一下,避免大家想我一样(唉,菜鸡本菜)还是老规矩在文末会附上完整代码,需要的小伙伴自取就好了,能帮助到你的话别忘了点赞关注喔~   郑重声明:本人目前仅在CSDN这一个平台发布文章,其他小伙伴如果想转载 或者引用请注明引用来源,未经许可不得直接搬运,请尊重创作人的劳动成果,谢谢! 一.需求分析   模拟登陆哔哩哔哩   网站链接: https://passport.bilibili.com

  • python selenium爬取斗鱼所有直播房间信息过程详解

    还是分析一下大体的流程: 首先还是Chrome浏览器抓包分析元素,这是网址:https://www.douyu.com/directory/all 发现所有房间的信息都是保存在一个无序列表中的li中,所以我们可以先获取一个装有li的element对象的列表,然后在对每个element逐一操作 分析斗鱼的翻页,有一个下一页按钮,是个li,class="dy-Pagination-item-custom" ,但是当烦到最后一页的时候,class="dy-Pagination-di

  • 浅谈python爬虫使用Selenium模拟浏览器行为

    前几天有位微信读者问我一个爬虫的问题,就是在爬去百度贴吧首页的热门动态下面的图片的时候,爬取的图片总是爬取不完整,比首页看到的少.原因他也大概分析了下,就是后面的图片是动态加载的.他的问题就是这部分动态加载的图片该怎么爬取到. 分析 他的代码比较简单,主要有以下的步骤:使用BeautifulSoup库,打开百度贴吧的首页地址,再解析得到id为new_list标签底下的img标签,最后将img标签的图片保存下来. headers = { 'User-Agent':'Mozilla/5.0 (Win

  • Python中Selenium模拟JQuery滑动解锁实例

    本文介绍了Python中Selenium模拟JQuery滑动解锁实例,分享给大家,也给自己留个笔记 滑动解锁一直做UI自动化的难点之一,我补一篇滑动解锁的例子,希望能给初做Web UI自动化测试的同学一些思路. 首先先看个例子. 当我手动点击滑块时,改变的只是样式: 1.slide-to-unlock-handle 表示滑块,滑块的左边距在变大(因为它在向右移动嘛!) 2.Slide-tounlock-progress 表示滑过之后的背景黄色,黄色的宽度在增加,因为滑动经过的地方都变黄了. 除些

随机推荐