1.9选课中心前两页课程名称打印到固定系统文件夹的某个csv文件中。
1 #-*- coding: UTF-8 -*- 2 #coding==utf-8 3 #from selenium.webdriver.support.wait import WebDriverWait 4 from selenium import webdriver 5 from time import sleep 6 import csv 7 8 outputFilePath = "E:\script\coursenames.csv" 9 10 #将读取的内容写入一个新的csv文档 11 def Savecsvfile(filePath): 12 csvFile2 = open(filePath, 'w', newline='') # 设置newline,否则两行之间会空一行 13 writer = csv.writer(csvFile2) 14 writer.writerow(['courseName']) 15 writer.writerows(outputString) 16 #csvfile.close() 17 18 if __name__=="__main__": 19 outputString=[] 20 n=0 21 driver = webdriver.Chrome() 22 driver.get("http://10.10.1.9/super/login/index.php") 23 driver.find_element_by_id("username").send_keys('candy30') 24 sleep(1) 25 driver.find_element_by_id("password").send_keys('1') 26 driver.find_element_by_id("loginbtn").click() 27 driver.find_element_by_link_text(u"选课中心").click() 28 29 while n<2: 30 n=n+1 31 print('您当前所在第%d页'% n) 32 links = driver.find_element_by_class_name("all_course_con").find_elements_by_tag_name("a") 33 print(links) 34 for link in links: 35 print(link.text) 36 if(link.text!='+选课'): 37 outputString.append([link.text]) 38 print(outputString) 39 driver.find_element_by_id('next').click() 40 driver.quit() 41 Savecsvfile(outputFilePath)
2.如果某个变量或者元素或者属性值中包含什么,执行什么操作。
site = 'http://www.outofmemory.cn/'
if "sharejs" in site:
if "sharejs" in site:
print('site contains sharejs')