因为方便性的原因,把方法封装了。因为芝士网的本身的问题,还是得要强制睡眠。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入webdriver方法,用来调用浏览器,需要自行下载好相应的驱动
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
# 这个是一些进阶的方法,比如拿到值
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
if __name__ == '__main__':
# 执行便会启动浏览器
mobileEmulation = {'deviceName': 'iPhone 6 Plus'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(chrome_options=options)
# 隐性等待和显性等待可以同时用,但要注意:等待的最长时间取两者之中的大者
# driver.implicitly_wait(5)
# 获取一个网页页面
driver.get("https://test.zhishinet.com/static_res/pages/StudentClient/www/#/startPage")
def get_element_by_method(driver, key, type, **kw):
try:
if type == 'xpath':
# 总共找10秒,0.5秒找一次,如果找到返回,找不到则抛异常
WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, key)))
element = driver.find_element_by_xpath(key)
elif type == 'id':
WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, key)))
element = driver.find_element_by_id(key)
elif type == 'link':
WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.LINK_TEXT, key)))
element = driver.find_element_by_link_text(key)
if element and 'method' in kw:
if kw['method'] == 'click':
element.click()
elif kw['method'] == 'submit':
element.submit()
elif kw['method'] == 'send_keys' and 'keys' in kw:
element.send_keys(kw['keys'])
else:
return element
except:
print('element can not find, key=', key)
print('kw=', kw)
finally:
pass
# 打开芝士网,输入账号密码,登录
get_element_by_method(driver, 'username', 'id', method='send_keys', keys='******')
get_element_by_method(driver, '//*[@id="login"]/div[2]/div/div/ons-list[2]/ons-list-item[2]/div/ons-row/ons-col[3]/input',
'xpath', method='send_keys', keys='******')
get_element_by_method(driver, '//*[@id="loginBtn"]/div', 'xpath', method='click')
# 打开微课堂
get_element_by_method(driver, '//*[@id="homePage"]/div[2]/ons-row/ons-col[4]', 'xpath', method='click')
sleep(2)
get_element_by_method(driver, '//*[@id="globalModal"]/div[2]/div/div/img', 'xpath', method='click')
get_element_by_method(driver, '//*[@id="globalModal"]/div[2]/div/div/img', 'xpath', method='click')
sleep(2)
# 选择英语、数学
get_element_by_method(driver, 'mic_center_subject', 'id', method='click')
get_element_by_method(driver, '//*[@id="microVideo"]/ons-popover/div[2]/div[1]/ons-list/ons-list-item',
'xpath', method='click')
# 选择第一个推荐视频观看
get_element_by_method(driver, '//*[@id="microVideo"]/div[2]/div/ons-list[1]/ons-row[2]/ons-carousel/div[1]'
'/ons-carousel-item[1]/div/div', 'xpath', method='click')
# 已经进入视频
# get_element_by_method(driver, '//*[@id="plv_container"]', 'xpath').click()
sleep(5)
# 进入视频后点赞
get_element_by_method(driver, '//*[@id="recommedPage"]/div[2]/ons-row/ons-col[1]/img', 'xpath', method='click')
# 取消点赞,先退出去,再进来点赞
get_element_by_method(driver, '//*[@id="microVideoPlayer"]/div[2]/div[1]/div', 'xpath', method='click')
sleep(3)
get_element_by_method(driver, '//*[@id="microVideo"]/div[2]/div/ons-list[1]/ons-row[2]/ons-carousel/div[1]'
'/ons-carousel-item[1]/div/div/div', 'xpath', method='click')
sleep(5)
get_element_by_method(driver, '//*[@id="recommedPage"]/div[2]/ons-row/ons-col[1]/img', 'xpath', method='click')
# 写评论 -- 评论打不开(设置设备默认为手机)
get_element_by_method(driver, '//*[@id="recommedPage"]/div[2]/ons-row/ons-col[2]/img', 'xpath', method='click')
# 光标先定位到输入框
get_element_by_method(driver, '//*[@id="microSuggestion"]/div[2]/textarea', 'xpath', method='click')
# 写评论
get_element_by_method(driver, '//*[@id="microSuggestion"]/div[2]/textarea', 'xpath', method='send_keys',
keys='视频非常不错\nhello,world!!!')
# 提交
get_element_by_method(driver, '//*[@id="microSuggestion"]/ons-toolbar/div[3]', 'xpath', method='click')
sleep(3)
# 收藏个性推荐视频,先退出,再点收藏,再进入视频
get_element_by_method(driver, '//*[@id="microVideoPlayer"]/div[2]/div[1]/div/span', 'xpath', method='click')
sleep(3)
get_element_by_method(driver, '//*[@id="microVideo"]/div[2]/div/ons-list[1]/ons-row[2]/ons-carousel/div[1]'
'/ons-carousel-item[1]/div/div/div/div[3]/img', 'xpath', method='click')
get_element_by_method(driver, '//*[@id="microVideo"]/div[2]/div/ons-list[1]/ons-row[2]/ons-carousel/div[1]'
'/ons-carousel-item[1]/div/div/div', 'xpath', method='click')
# 筛选视频
sleep(3)
# 退出
get_element_by_method(driver, '//*[@id="microVideoPlayer"]/div[2]/div[1]/div', 'xpath', method='click')
sleep(3)
# 点击筛选
get_element_by_method(driver, '//*[@id="microVideo"]/ons-toolbar/div[3]/ons-button[1]', 'xpath', method='click')
sleep(2)
# 点击我知道了,选择有理数
get_element_by_method(driver, '//*[@id="globalModal"]/div[2]/div/div/img', 'xpath', method='click')
get_element_by_method(driver, '//*[@id="microVideoList"]/div[2]/ons-list/div[2]/div[4]', 'xpath', method='click')
sleep(3)
# 搜索视频,点击搜索
get_element_by_method(driver, '//*[@id="microVideo"]/ons-toolbar/div[3]/ons-button[2]', 'xpath', method='click')
# 点击框,
get_element_by_method(driver, '//*[@id="searchText"]', 'xpath', method='click')
# 输入文字
get_element_by_method(driver, '//*[@id="searchText"]', 'xpath', method='send_keys', keys='微积分')
# 搜索
get_element_by_method(driver, '//*[@id="microVideoSearch"]/ons-toolbar/div[3]', 'xpath', method='click')
get_element_by_method(driver, '//*[@id="globalModal"]/div[2]/div/div/img', 'xpath', method='click')