標籤:java 技術分享 select span lis app print 顯示 driver
未經作者允許,禁止轉載!python selenium webdriver處理迴圈點擊
迴圈點擊主要是針對一些同類的選擇按鈕,或者下拉選項,或者是選擇頁碼~~~~~~
如下面兩張圖片顯示的將年份進行迴圈選擇,和將滑鼠懸浮迴圈選擇
下面是用python寫的 selenium webdriver處理迴圈點擊代碼,相關的java代碼隨後在發表
from selenium import webdriverimport timefrom selenium.webdriver.common.action_chains import ActionChainsdriver=webdriver.Chrome()driver.get("http://toyota-oss:[email protected]/tcom-apps/vapi-preview/console.html")#alert = driver.switch_to_alert() 如果是第一次開啟上面的連結,就需要alert.accept(),但是從第二次之後保留了cookie就不再有彈窗#alert.accept()driver.maximize_window()time.sleep(2)print "進入toyota首頁" #total_year=len(driver.find_element_by_id(‘year‘).find_element_by_tag_name(‘option‘))------報錯:WebElement沒有len屬性#print "年份的個數是"+total_year#time.sleep(2)#years=driver.find_element_by_id(‘year‘).find_element_by_tag_name(‘option‘)----------報錯:WebElement沒有迴圈屬性#for year in years:# year.click()# time.sleep(1)---------------本來打算用這個迴圈很方便的,但是總是報錯,只能用下面的笨方法了!modelyear=driver.find_element_by_xpath("//select[@id=‘year‘]/option[1]")year2017=driver.find_element_by_xpath("//select[@id=‘year‘]/option[2]")year2016=driver.find_element_by_xpath("//select[@id=‘year‘]/option[3]")year2015=driver.find_element_by_xpath("//select[@id=‘year‘]/option[4]")year2014=driver.find_element_by_xpath("//select[@id=‘year‘]/option[5]")year2013=driver.find_element_by_xpath("//select[@id=‘year‘]/option[6]")yearList=[modelyear,year2017,year2016,year2015,year2014,year2013]for year in yearList: year.click() time.sleep(1)time.sleep(3)driver.find_element_by_xpath("//*[@id=‘console‘]/div[6]/input").click()time.sleep(4)Content1=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[1]")Content2=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[2]")Content3=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[3]")Content4=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[4]")Content5=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[5]")Content6=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[6]")ContentList=[Content1,Content2,Content3,Content4,Content5,Content6]for Content in ContentList: Content.click() time.sleep(1)print "迴圈點擊結束"time.sleep(2)Content1=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[1]")Content2=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[2]")Content3=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[3]")Content4=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[4]")Content5=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[5]")Content6=driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[6]")ContentList=[Content1,Content2,Content3,Content4,Content5,Content6]for Content in ContentList:#above=driver.find_element_by_id("hao123-star") ActionChains(driver).move_to_element(Content).perform() time.sleep(1)print "迴圈懸浮結束"
total_year=len(driver.find_element_by_id(‘year‘).find_element_by_tag_name(‘option‘))-------------報錯:WebElement沒有len屬性print "年份的個數是"+total_yeartime.sleep(2)years=driver.find_element_by_id(‘year‘).find_element_by_tag_name(‘option‘)-----------------------報錯:WebElement沒有迴圈屬性for year in years: year.click() time.sleep(1)
不知道為什麼總是報錯,我有點開始懷疑人生了!慢慢研究吧,有結果了在分享給大家!
above=driver.find_element_by_id("hao123-star")
ActionChains(driver).move_to_element(above).perform()
這段代碼是:滑鼠懸浮
webdriver處理迴圈點擊