webdriver處理迴圈點擊

來源:互聯網
上載者:User

標籤: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處理迴圈點擊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.