Use selenium to automate testing of Xuedutong

I. Introduction

1 Install selenium and webdriver

selenium is a third-party library for python, just use pip.

For webdriver, I use Edge, go to the official website and download it.

Microsoft Edge WebDriver - Microsoft Edge Developer

If you are using other browsers, such as Chrome, etc., the same operation

After downloading,

Some tutorials on the Internet require operations such as registering environment variables. It feels unnecessary

Unzip webdriver and get msedgedriver.exe

Put this below the python interpreter, mine looks like this.

C:\Users\520\AppData\Local\Programs\Python\Python310。

Put it in the python310 folder.

2 po design pattern

The po design pattern, to put it bluntly, is to re-encapsulate selenium.

Simple keyword encapsulation

    def locates(self, name, value):
        return self.driver.find_elements(name, value)
    def xpaths(self, value):
        return self.locates(By.XPATH, value)
"""
这段代码本质上就是对find_elements的封装
再结合对xpath进行的封装。
"""

There are many encapsulations of my own in the text, hahahahaha,

Finally give the used one.

Two main texts

1 Login

You need to enter your mobile phone number and password.

class Xuexitong:
    def __init__(self,n=1):
        self.driver=web()
        self.n=n
# n 后面再说什么意思
# 初始化,得到webdriver对象
    def login(self):
# open 就是对get方法的封装
        self.driver.open('https://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com')
        self.driver.i_put('phone','xxxxx')
# i_put 对id,find_element,send_keys的封装
        self.driver.i_put('pwd','xxxxx')
        self.driver.i_click('loginBtn')
# i_click是id和click的封装

The above process is:

Open the official website->Enter your mobile phone number>Enter your password->Click the OK button

2 Select a course

    def click_course(self):
        self.driver.wait(1)
# 等待1s time.sleep的封装
        self.driver.switch_frame('frame_content')
# 进入框架,很重要,不然会找不到元素。
# self.n 就是课程的序号,从1到最后,选择自己需要点击的课程
        self.driver.x_click(f'//*[@class="course-list"]/li[{self.n}]/div/a')

3 Determine the number of courses to click)

Some course numbers are displayed as 1, and some are 2. Obviously exclude 1 and click 2.

    def choose_window(self):
# 需要先切换窗口,选择句柄,不妨称为列表窗口
# 不然还是再原来的窗口
        self.driver.switch_handle(-1)
        self.driver.wait(1)
# 进入列表窗口的框架
        self.driver.switch_frame('frame_content-zj')
    def get_course_nums(self):
        course_nums=[]
        self.choose_window()
        course=self.driver.xpaths('//*[@class="chapter_unit"][3]/div[2][1]/ul/li/div/div/div[3]/div/span[1]')
# couser 就是用了xpaths, 得到课程数,返回的是元素
        for t in course:
            value=self.driver.get_text(t)
# 得到内容
            course_nums.append(value)
# 比如是这样的[1,1,1,1,1,1,1,1,2,2,2,2,2,2,2]等的
# 也有可能有3。
        return course_nums

4 Click on the chapter you want to enter

    def main(self):
        self.login()
        self.click_course()
        nums=self.get_course_nums()
# 前面的操作。得到课程数
        for i,t in enumerate(nums):
# 循环
            if t=='2':
# 对章节数的进行后续操作,play操作
                self.play(i+1)

5 play operations

    def play(self,num):
        self.choose_window()
# 选择窗口
        self.start_play(num)
# start_play操作

6 start_play operation

    def start_play(self,num):
        self.driver.x_click(f'//*[@class="chapter_unit"][3]/div/ul/li[{num}]/div/div/div[3]')
# 点击跳转
        self.driver.switch_handle(-1)
# 选择窗口
        self.driver.switch_frame('iframe')
# 进入框架
        self.driver.switch_frame('//*[@id="ext-gen1049"]/iframe')
# 进入框架
        self.driver.wait(2)
        self.driver.x_click('//*[@class="vjs-big-play-button"]')
# 点击播放按钮
        while 1:
# 等待播放完成
            flag = self.get_new_num()
            if flag:
                break
        return False
    def get_new_num(self):
        self.driver.wait(10)
        div = self.driver.xpath(f'//*[@id="reader"]/div')
        get_class = self.driver.get_attr(div, 'class')
        tag_class = 'video-js vjs-default-skin vjs-big-play-centered vjs-controls-enabled vjs-workinghover vjs-v7 vjs-has-started video-dimensions vjs-user-inactive vjs-paused vjs-ended'
        tag_class1='video-js vjs-default-skin vjs-big-play-centered vjs-controls-enabled vjs-workinghover vjs-v7 vjs-has-started video-dimensions vjs-paused vjs-ended vjs-user-inactive'
# 播放完成或有某些标签的变化。仔细寻找
        if get_class == tag_class or get_class==tag_class1:
            self.driver.switch_default()
            self.driver.x_click('//*[@id="contentFocus"]/i')
# 退出按钮
            self.driver.wait(4)
            return True

7 Select window

    def choose_window(self):
        self.driver.switch_handle(-1)
        self.driver.wait(1)
        self.driver.switch_frame('frame_content-zj')

The above operation---select the video to be tested->click play->wait for playback to complete->exit

The specific actual situation depends on your own operation and adapt to changes.

Three source codes

from init import web
# 封装导包
class Xuexitong:
    def __init__(self,n=1):
        self.driver=web()
        self.n=n
        self.main()
    def login(self):
        self.driver.open('https://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com')
        self.driver.i_put('phone','xx')
        self.driver.i_put('pwd','xx')
        self.driver.i_click('loginBtn')
    def click_course(self):
        self.driver.wait(1)
        self.driver.switch_frame('frame_content')
        self.driver.x_click(f'//*[@class="course-list"]/li[{self.n}]/div/a')
    def get_course_nums(self):
        course_nums=[]
        self.choose_window()
        course=self.driver.xpaths('//*[@class="chapter_unit"][3]/div[2][1]/ul/li/div/div/div[3]/div/span[1]')
        for t in course:
            value=self.driver.get_text(t)
            course_nums.append(value)
        return course_nums
    def main(self):
        self.login()
        self.click_course()
        nums=self.get_course_nums()
        for i,t in enumerate(nums):
            if t=='2':
                self.play(i+1)
    def play(self,num):
        self.choose_window()
        self.start_play(num)
    def get_new_num(self):
        self.driver.wait(10)
        div = self.driver.xpath(f'//*[@id="reader"]/div')
        get_class = self.driver.get_attr(div, 'class')
        tag_class = 'video-js vjs-default-skin vjs-big-play-centered vjs-controls-enabled vjs-workinghover vjs-v7 vjs-has-started video-dimensions vjs-user-inactive vjs-paused vjs-ended'
        tag_class1='video-js vjs-default-skin vjs-big-play-centered vjs-controls-enabled vjs-workinghover vjs-v7 vjs-has-started video-dimensions vjs-paused vjs-ended vjs-user-inactive'
        if get_class == tag_class or get_class==tag_class1:
            self.driver.switch_default()
            self.driver.x_click('//*[@id="contentFocus"]/i')
            self.driver.wait(4)
            return True
    def start_play(self,num):
        self.driver.x_click(f'//*[@class="chapter_unit"][3]/div/ul/li[{num}]/div/div/div[3]')
        self.driver.switch_handle(-1)
        self.driver.switch_frame('iframe')
        self.driver.switch_frame('//*[@id="ext-gen1049"]/iframe')
        self.driver.wait(2)
        self.driver.x_click('//*[@class="vjs-big-play-button"]')
        while 1:
            flag = self.get_new_num()
            if flag:
                break
        return False
    def choose_window(self):
        self.driver.switch_handle(-1)
        self.driver.wait(1)
        self.driver.switch_frame('frame_content-zj')

def main():
    Xuexitong()

Four encapsulated codes

See my github repository

encapsulated code

Guess you like

Origin blog.csdn.net/qq_63401240/article/details/130128141