本次我采用Python+selenium写了一个自动播放视频的脚本。

从账号密码登录,切换一个个window,再到切换一层层frame,最终点击播放按钮的过程。

  1. 导入包

    1
    2
    3
    4
    5
    6
    7
    from selenium import webdriver 
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    import time
  2. 将webdriver静音

    1
    2
    3
    4
    options = webdriver.ChromeOptions()
    options.add_argument("--mute-audio")
    # 打开浏览器
    browser = webdriver.Chrome(options=options)
  3. 模拟浏览器,顺便进行反屏蔽

    1
    2
    3
    4
    5
    browser = webdriver.Chrome()
    # 反屏蔽
    browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
    })
  4. 获取url

    1
    2
    url = "http://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com" #这里改成自己学校的学习通登录地址
    browser.get(url)
  5. 输入账号密码,再登录

    1
    2
    3
    4
    5
    6
    7
    8
    inp='your_account'
    inp2='your_password'
    username=browser.find_element(By.XPATH,'//*[@id="phone"]')
    password=browser.find_element(By.XPATH,'//*[@id="pwd"]')
    username.send_keys(inp)
    password.send_keys(inp2)
    login=browser.find_element(By.XPATH,'//*[@id="loginBtn"]')
    login.click()
  6. 此时,网页中会有一个iframe,导致selenium无法正常读取iframe里的html代码,这里用到了selenium的switch_to.frame()的方法

    • 如果frame中有id或者name属性,那么我们直接传入他们之一的字符串就可以了

      1
      browser.switch_to.frame("frame_content")
    • 如果没有以上两个属性,我们就用find_element()获取这个frame的元素,把他传入switch_to.frame()

      1
      2
      element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ext-gen1044"]/iframe')))
      browser.switch_to.frame(element)
  7. 如果click()某个元素之后,弹出一个新窗口,那么selenium同样无法获取新窗口的元素,这里就用到了switch_to.window()的方法

    以下方法就是关闭第一个窗口切换到第二个窗口

    1
    2
    3
    4
    5
    6
    7
    handles = browser.window_handles  # 获取当前窗口句柄集合
    print(handles)
    for handle in handles: # 切换窗口
    if handle != browser.current_window_handle:
    print('switch to second window', handle)
    browser.close() # 关闭第一个窗口
    browser.switch_to.window(handle) # 切换到第二个窗口

完整代码1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from selenium import webdriver  # 导入库
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--mute-audio")

browser = webdriver.Chrome(options=chrome_options)
# 反屏蔽
browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})

# 设置延迟等待对象
wait=WebDriverWait(browser, 10)

url = "http://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com" #这里改成自己学校的学习通登录地址
browser.get(url)
browser.maximize_window() # 窗口最大化

inp='15975027211'
inp2='@Qq2670387848'
username=browser.find_element(By.XPATH,'//*[@id="phone"]')
password=browser.find_element(By.XPATH,'//*[@id="pwd"]')
username.send_keys(inp)
password.send_keys(inp2)

login=browser.find_element(By.XPATH,'//*[@id="loginBtn"]')
login.click()
time.sleep(2)
course=browser.find_element(By.XPATH,'//*[@id="first57734"]')
course.click()

browser.switch_to.frame("frame_content")

course1=wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="c_220056940"]/div[2]/h3/a/span')))
course1.click()

handles = browser.window_handles # 获取当前窗口句柄集合
print(handles)
for handle in handles: # 切换窗口
if handle != browser.current_window_handle:
print('switch to second window', handle)
browser.close() # 关闭第一个窗口
browser.switch_to.window(handle) # 切换到第二个窗口

browser.switch_to.frame("frame_content-zj")

item=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#fanyaChapter > div > div.chapter_body.xs_table > div.chapter_td > div:nth-child(2) > div.catalog_level > ul > li:nth-child(1) > div > div > div.catalog_name')))
item.click()

while 1:
handles = browser.window_handles # 获取当前窗口句柄集合
print(handles)
for handle in handles: # 切换窗口
if handle != browser.current_window_handle:
print('switch to second window', handle)
browser.close() # 关闭第一个窗口
browser.switch_to.window(handle) # 切换到第二个窗口
time.sleep(2)
browser.switch_to.frame("iframe")

element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ext-gen1044"]/iframe')))
browser.switch_to.frame(element)

play=wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="video"]/button')))
play.click()

time.sleep(180)
browser.refresh()

例二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains

def create_web_driver(url):
options = webdriver.ChromeOptions()
options.add_argument("--mute-audio")
# 打开浏览器
browser = webdriver.Chrome(options=options)

#反屏蔽
browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
browser.get(url)
browser.maximize_window()
return browser

def login(browser,user_name,password):
wait = WebDriverWait(browser, 10)
login_button = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="y-user-account"]/span/a[2]')))
login_button.click()
# 填写账号
wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="account-txt"]'))).send_keys(user_name)
# 填写密码
browser.find_element(By.XPATH, '//*[@id="password-txt"]').send_keys(password)
# 点击登录
browser.find_element(By.XPATH,'//*[@id="login-btn"]').click()


def action(browser):
wait = WebDriverWait(browser, 10)
a=wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="YOOC_MENU"]')))
a.click()

handles = browser.window_handles # 获取当前窗口句柄集合
print(handles)
for handle in handles: # 切换窗口
if handle != browser.current_window_handle:
print('switch to second window', handle)
browser.close() # 关闭第一个窗口
browser.switch_to.window(handle) # 切换到第二个窗口
time.sleep(3)
# 定位元素
hover_element = browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[1]')
# 对该元素执行悬停操作
ActionChains(browser).move_to_element(hover_element).perform()
# 等待几秒看看效果
time.sleep(3)

browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[2]/ul/li[2]/a').click()
browser.find_element(By.XPATH, '/html/body/div[2]/div[2]/table/tbody/tr/td/table[2]/tbody/tr/td[2]/a/h1').click()
browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div/a').click()

i = 0
while 1:
browser.find_element(By.XPATH, f'/html/body/div[3]/div/div[2]/ul/li[{26+i}]/a/h4').click()
time.sleep(8)
# 定位元素
hover_element = browser.find_element(By.XPATH, '//*[@id="video"]')
# 对该元素执行悬停操作
ActionChains(browser).move_to_element(hover_element).perform()
# 等待几秒看看效果
time.sleep(3)
browser.find_element(By.XPATH, '//*[@id="video"]').click()
time.sleep(20*60)
i=i+1
print(i)

if __name__ == '__main__':
user_name=''
password=''
brower=create_web_driver("https://www.yiban.cn/")
login(brower,user_name,password)
action(brower)