自动播放视频并录屏保存的python脚本

`from time import sleep
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.support.ui import Select, webdriverwait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pyautogui
import pyperclip

driver = webdriver.Firefox()

打开网页

driver.get('https://www.luffycity.com/')

登录

窗口最大化

driver.maximize_window()

关闭广告弹窗,

driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/img[1]').click()

然后点击登录按钮

webdriverwait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div/div/header/div/div/div[2]/div[2]/span')))
driver.find_element_by_xpath('/html/body/div[1]/div/div/header/div/div/div[2]/div[2]/span').click()

输入用户名,密码

driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div[1]/input').clear()
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div[1]/input').send_keys('username')
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div[2]/input').clear()
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div[2]/input').send_keys(
'password')

点击登录

driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/button').click()
sleep(3)

打开一个课程链接

driver.get('https://www.luffycity.com/play/13735')

循环下面的步骤

def record_screen(count):
for i in range(count):
# 点击一次刷新按钮
driver.refresh()
# 当全屏按钮出现时,点击全屏按钮,
webdriverwait(driver, 10).until(EC.visibility_of_element_located(
(By.XPATH, '/html/body/div[1]/div/div/div/div[1]/div[2]/div/div/div[2]/div[2]/div[2]/div[8]/button')))
driver.find_element_by_xpath(
'/html/body/div[1]/div/div/div/div[1]/div[2]/div/div/div[2]/div[2]/div[2]/div[8]/button').click()
# 点击开始
driver.find_element_by_xpath('/html/body/div[1]/div/div/div/div[1]/div[2]/div/div/div[1]/div[4]/span').click()
# 通过快捷键ctrl+F1开始录屏,用autoit切换到浏览器
pyautogui.hotkey('ctrl', 'F1')
# 等待5s,查询class="section active"下面的h5的名称,并copy,

1.313 第89天:Flask初始化配置

sleep(5)
video_name = driver.find_element_by_xpath("//div[@class='section active']/section/h5").get_attribute(
'textContent')
video_name_postfix = video_name + '.wmv'
pyperclip.copy(video_name_postfix)
# 每过10s检查一次pv-time-current时间,如果pv-time-current时间=pv-time-duration,通过快捷键ctrl+F2结束录屏,并退出while循环
# 00:21,38:44
while True:
pv_time_current = driver.find_element_by_class_name("pv-time-current").get_attribute('textContent')
pv_time_duration = driver.find_element_by_class_name("pv-time-duration").get_attribute('textContent')
if pv_time_current == pv_time_duration:
print('视频播放完成:', video_name_postfix)
pyautogui.hotkey('ctrl', 'F2')
break
else:
sleep(10)
# paste上面copy的名称+wmv后缀,用autoit切换到浏览器,按ESC退出全屏
sleep(2)
pyautogui.hotkey('ctrl', 'v')
pyautogui.hotkey('enter')
pyautogui.hotkey('alt', 'tab')
sleep(1)
pyautogui.hotkey('esc')
sleep(1)
# 检查是否有反馈弹窗,如果有就点击关闭按钮,关闭按钮
pop_window_Feedback = '/html/body/div[1]/div/div/div/div[1]/div[3]/div/p[1]/img'
pop_window_next = '/html/body/div[1]/div/div/div/div[1]/div[3]/button'
try:
# driver.find_element_by_xpath(pop_window_Feedback)
driver.find_element_by_xpath(pop_window_Feedback).click()
driver.find_element_by_xpath(pop_window_next).click()
except:
# 如果没有反馈弹窗,就点击下一节按钮,
driver.find_element_by_xpath(pop_window_next).click()
# count减1
# count -= 1
print(i)

record_screen(200)
`

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...