標籤:div pass 解決 file next data- body argument not
selenium2 python範例
下面指令碼的功能是:開啟Google瀏覽器--》跳轉到某個網址--》輸入使用者名稱和密碼登入--》讀取頁面內的資料並求和。
1 # coding=utf-8 #編碼聲明 2 import time 3 from selenium import webdriver 4 5 # 跳轉到首頁 6 driver = webdriver.Chrome() # Optional argument, if not specified will search path. 7 driver.implicitly_wait(15) 8 driver.get(‘http://xxx.xxx.xxx‘) # 網址 9 # driver.maximize_window()10 time.sleep(5) # Let the user actually see something!11 loginButton1 = driver.find_element_by_id(‘login‘) # 通過id定位元素12 loginButton1.click()13 driver.find_element_by_id(‘username‘).send_keys(‘xxxxxx‘) # 使用者名稱14 driver.find_element_by_id(‘password‘).send_keys(‘yyyyyy‘) # 密碼15 time.sleep(5)16 driver.find_element_by_xpath(‘//button[@type="submit"]‘).click() # 通過xpath定位元素17 time.sleep(5) # Let the user actually see something!18 19 # 設定機構20 driver.find_element_by_xpath("//*[@id=‘profile-messages‘]/a/b").click()21 driver.find_element_by_xpath("//*[@id=‘profile-messages‘]/ul/li[3]/a").click()22 time.sleep(5)23 # 作品排行24 driver.find_element_by_xpath("//*[@id=‘articles_Rank‘]/a").click()25 # 全部媒體26 driver.find_element_by_xpath("//*[@id=‘media_0‘]").click()27 # 設定時間28 driver.find_element_by_xpath("//*[@id=‘reservation‘]").click()29 driver.find_element_by_xpath("//div[@class=‘calendar left‘]//th[@class=‘prev available‘]").click()30 driver.find_element_by_xpath("//div[@class=‘calendar left‘]//td[@data-title=‘r0c6‘]").click() # 10月1號31 driver.find_element_by_xpath("//div[@class=‘calendar right‘]//th[@class=‘prev available‘]").click()32 driver.find_element_by_xpath("//div[@class=‘calendar right‘]//td[@data-title=‘r5c1‘]").click() # 10月31號33 driver.find_element_by_xpath("//body/div[8]/div[1]/div/button[1]").click()34 35 # 讀取目標並求值36 time.sleep(1) # 加入延時,解決“click操作後立刻尋找的元素使用element.text,報stale element錯誤”的問題37 total = int(0)38 elementList = driver.find_elements_by_xpath("//*[@id=‘articleContent‘]//td[5]")39 for element in elementList:40 text2 = element.text41 value = int(text2)42 total = total + value43 hasNext = False44 while 1:45 buttonList = driver.find_elements_by_xpath("//*[@id=‘page‘]//a")46 pageButton = buttonList[len(buttonList)-1]47 if pageButton.text == "Next":48 hasNext = True49 driver.execute_script("arguments[0].scrollIntoView();", pageButton) # 執行js語句,通過滾動頁面,將不在視野內的元素挪到視野內。50 pageButton.click()51 else:52 hasNext = False53 driver.quit()54 break55 if hasNext:56 time.sleep(1)57 elementList = driver.find_elements_by_xpath("//*[@id=‘articleContent‘]//td[5]")58 for element in elementList:59 text1 = element.text60 value = int(text1)61 total = total + value62 print "value: " + str(value)63 print "total: " + str(total)64 65 66 #67 driver.quit()
selenium2 python範例