51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1142|回复: 0
打印 上一主题 下一主题

[原创] Selenium实现自动小实验!

[复制链接]
  • TA的每日心情
    无聊
    12 小时前
  • 签到天数: 935 天

    连续签到: 2 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-9-27 16:20:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    需求:
      某大学远程教育自动刷课脚本,每20分钟刷一次。
      ·修改学习平台和超级鹰打码平台的用户名密码。
      · 安装对应的模块
        - pip install PIL
        - pip install selenium
      · 下载chromderiver驱动,放到d:/chromedriver.exe.
        -注意:电脑分辨率必须为100%,不然无法调用.
      · 执行脚本
    1. <font size="3"> from selenium import webdriver

    2.   from hashlib import md5

    3.   from PIL import Image

    4.   import time

    5.   import requests

    6.   class Chaojiying_Client(object):

    7.       '''

    8.       超级鹰调用类接口

    9.       '''

    10.       def __init__(self, username, password, soft_id):

    11.           self.username = username

    12.           password =  password.encode('utf8')

    13.           self.password = md5(password).hexdigest()

    14.           self.soft_id = soft_id

    15.           self.base_params = {

    16.               'user': self.username,

    17.               'pass2': self.password,

    18.               'softid': self.soft_id,

    19.           }

    20.           self.headers = {

    21.               'Connection': 'Keep-Alive',

    22.               'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',

    23.           }

    24.       def PostPic(self, im, codetype):

    25.           """

    26.           im: 图片字节

    27.           codetype: 题目类型 参考 http://www.chaojiying.com/price.html

    28.           """

    29.           params = {

    30.               'codetype': codetype,

    31.           }

    32.           params.update(self.base_params)

    33.           files = {'userfile': ('ccc.jpg', im)}

    34.           r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)

    35.           return r.json()

    36.       def ReportError(self, im_id):

    37.           """

    38.           im_id:报错题目的图片ID

    39.           """

    40.           params = {

    41.               'id': im_id,

    42.           }

    43.           params.update(self.base_params)

    44.           r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)

    45.           return r.json()

    46.   class Course_refush():

    47.       '''

    48.       刷某学习平台的课程点击次数

    49.       '''

    50.       def __init__(self,chromdriver='d:/chromedriver.exe',codepath='./maincode.png',username='3101111111111035',password='111111',codeuser='username',codepass='password',codeid='99999'):

    51.           self.codepath = codepath

    52.           self.username = username

    53.           self.password = password

    54.           self.chromdriverpath = chromdriver

    55.           self.codeuser = codeuser

    56.           self.codepass = codepass

    57.           self.codeid = codeid

    58.       def analysis_code(self,imgPath, imgType):

    59.           chaojiying = Chaojiying_Client(self.codeuser, self.codepass, self.codeid)  # 用户中心>>软件ID 生成一个替换 96001

    60.           im = open(imgPath, 'rb').read()      # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//

    61.           return chaojiying.PostPic(im, imgType)['pic_str']

    62.       def study_course(self,url):

    63.           bro = webdriver.Chrome(executable_path=self.chromdriverpath)

    64.           bro.get(url)

    65.           #输入用户名密码

    66.           get_user_elem = bro.find_element_by_xpath('//*[@id="studentnum"]')

    67.           get_user_elem.send_keys(self.username)

    68.           get_pass_elem = bro.find_element_by_xpath('//*[@id="stupwd"]')

    69.           get_pass_elem.send_keys(self.password)

    70.           #解析验证码图片地址

    71.           bro.save_screenshot(self.codepath)

    72.           code_img_tag = bro.find_element_by_xpath('// *[ @ id = "VCodeImg"]')

    73.           location = code_img_tag.location

    74.           size = code_img_tag.size

    75.           rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']),

    76.                     int(location['y'] + size['height']))  # 写成我们需要截取的位置坐标

    77.           i = Image.open(self.codepath)  # 打开截图

    78.           frame4 = i.crop(rangle)  # 使用Image的crop函数,从截图中再次截取我们需要的区域

    79.           frame4.save(self.codepath)

    80.           #识别验证码

    81.           code_result = self.analysis_code(self.codepath, 1902)

    82.           #输入验证码并记住密码,点击登录

    83.           get_code_elem = bro.find_element_by_xpath('//*[@id="vcode"]')

    84.           get_code_elem.send_keys(code_result)

    85.           get_retain_elem = bro.find_element_by_xpath('// *[ @ id = "ckbremeber"]')

    86.           get_retain_elem.click()

    87.           get_login_elem = bro.find_element_by_xpath('//*[@id="btnlogin"]')

    88.           get_login_elem.click()

    89.           #不关注公众号

    90.           get_nofollow_elem = bro.find_element_by_xpath('//*[@id="myModal"]/div[3]/a')

    91.           get_nofollow_elem.click()

    92.           homepage = bro.current_window_handle

    93.           #获取需要学习的课程链接

    94.           get_curselink_elem = bro.find_elements_by_xpath('//*[@id="tab4"]/table/tbody//a[@href]')

    95.           curse_links = []

    96.           for curselink in get_curselink_elem:

    97.               resultlink = curselink.get_attribute('href')

    98.               if 'schoolwork' in resultlink:

    99.                   pass

    100.               else:

    101.                   curse_links.append(resultlink)

    102.           # 循环访问需要学习的课程链接

    103.           js = "window.open('{}','_blank');"

    104.           for count in range(15):

    105.               for getcurselink in curse_links:

    106.                   bro.execute_script(js.format(getcurselink))

    107.                   time.sleep(1)

    108.               subhandles = bro.window_handles

    109.               for newhandle in subhandles:

    110.                   if newhandle != homepage:

    111.                       bro.switch_to.window(newhandle)

    112.                       bro.close()

    113.                       time.sleep(1)

    114.               bro.switch_to_window(homepage)

    115.               bro.minimize_window()

    116.               time.sleep(1200)

    117.           bro.quit()

    118.   if __name__ == '__main__':

    119.       #打码平台

    120.       codeuser = 'username'

    121.       codepass = 'password'

    122.       codeid = 'codeid'

    123.       #学习平台用户密码

    124.       username = 'username'

    125.       password = 'password'

    126.       url = 'http://netstu.snnu.net/Login.aspx'

    127.       curse_instance = Course_refush(username=username,password=password,codeuser=codeuser,codepass=codepass,codeid=codeid,chromdriver='d:/chromedriver.exe')

    128.       curse_instance.study_course(url)</font>
    复制代码



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-23 21:41 , Processed in 0.071512 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表