python Selenium学习
mport random from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from selenium.common.exceptions import NoSuchElementException,StaleElementReferenceException,ElementNotVisibleException,TimeoutException,WebDriverException import time import os import sys USER_NAME='137' PASS_WORD='' dr=webdriver.Firefox()#Chrome()# dr.maximize_window() fp='137.txt' urls=[] def Login(): home='' dr.get(home) wait=WebDriverWait(dr,10) locator=(By.ID,'username') try: username=wait.until(EC.visibility_of_element_located(locator)) username.send_keys(USER_NAME) except: print('未发现用户表单控件') try: password=dr.find_element_by_id('password') password.send_keys(PASS_WORD) except: print('未发现密码表单控件') try: loginBtn=dr.find_element_by_id('loginBtn') loginBtn.click() except: print('未发现登录按钮') wait=WebDriverWait(dr,10) locator=(By.XPATH,'//a[text()="2019年第2期"]') try: tag=wait.until(EC.visibility_of_element_located(locator)) #tag.click() return True except: return False def AccessCoure(): wait=WebDriverWait(dr,10) locator=(By.XPATH,'//a[text()="2019年第2期"]') try: tag=wait.until(EC.visibility_of_element_located(locator)) tag.click() print('进入课程页面') except: pass def ExpandCourses(): time.sleep(3) dr.switch_to_window(dr.window_handles[1]) treeOnes=dr.find_elements_by_xpath('//div[@class="tier-tree-one "]/p[@class="tier1 has-children"]/i') print('章数量'+str(len(treeOnes))) for treeone in treeOnes: treeone.click() treeTwos=dr.find_elements_by_xpath('//div/div[@class="tier-tree-two tier-tree-inner "]/p/i') print('节数量'+str(len(treeTwos))) for treeTwo in treeTwos: treeTwo.click() unfinishs=dr.find_elements_by_xpath('//font[text()="未完成"]/parent::span/parent::p/span[contains(text(),"学习内容")]/following-sibling::span/a') print('未完成数量'+str(len(unfinishs))) for a in unfinishs: print(a.text) if len(unfinishs)>0: unfinishs[0].click() return True else: return False def BeginStudy(): time.sleep(3) #dr.switch_to_window(dr.window_handles[1]) vdo=dr.find_element_by_id('ckplayer_a1') vdo.click() duration=dr.execute_script("return arguments[0].duration;", vdo)#playbackRate=5 duration=dr.execute_script("return arguments[0].playbackRate=2;", vdo) print("该视频的观看时长为:" + str(duration) +"秒!" ) time.sleep(int(duration)+1000)# dr.back() #单击确认完成呢 if __name__=='__main__': print(Login()) AccessCoure() #GetCourseUrls() while ExpandCourses(): BeginStudy() #BeginStudy()