首页 > 代码库 > webdriver处理循环点击

webdriver处理循环点击

未经作者允许,禁止转载!python selenium webdriver处理循环点击

循环点击主要是针对一些同类的选择按钮,或者下拉选项,或者是选择页码~~~~~~

如下面两张图片显示的将年份进行循环选择,和将鼠标悬浮循环选择

技术分享

技术分享

下面是用python写的 selenium webdriver处理循环点击代码,相关的java代码随后在发表

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains

driver=webdriver.Chrome()
driver.get("http://toyota-oss:changeit@test3.tcom.aws.toyota.com/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_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)
不知道为什么总是报错,我有点开始怀疑人生了!慢慢研究吧,有结果了在分享给大家!

above=driver.find_element_by_id("hao123-star")
ActionChains(driver).move_to_element(above).perform()

这段代码是:鼠标悬浮

webdriver处理循环点击