Selenium_WebDriver操作iFrame日历框和复选框_Java
iFrame日歷框
頁面上遇到iFrame元素時(shí),先用findElement找到這個(gè)iFrame元素,然后再WebDriver.switchTo().frame(calFrame)。在iFrame里操作完成后,記得再切換會(huì)原來的窗體WebDriver.switchTo().defaultContent()
復(fù)選框
復(fù)選框是很常見的網(wǎng)頁頁面元素,操作起來也很easy,findElement找到復(fù)選框元素,再使用普通的.click()方式就可以選中或不選中這個(gè)checkbox。checkbox.isSelected()則能夠查看該復(fù)選框是否被選中。
我自己在操作的過程中遇到一個(gè)問題,明明這個(gè)元素是能夠點(diǎn)擊的,有onclick屬性,可是ChromeDriver提示說這個(gè)元素unclickable…百思不得其解,最后切換回FirefoxDriver,一切正常,能夠操作= =#
另外,測(cè)試過程中假設(shè)要用到Chrome的話。除了要預(yù)先安裝好Chrome瀏覽器。還須要下載ChromeDriver,官方下載地址例如以下:
http://chromedriver.storage.googleapis.com/index.html?path=2.16/
下載下來之后放哪里都無所謂事實(shí)上,僅僅要在程序里設(shè)置一下它的路徑就能夠正常使用了
代碼塊
package CalendarCheckboxOperation;import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.List;import java.util.concurrent.TimeUnit;public class CalendarCheckboxOperation {private WebDriver dr;private String url;public CalendarCheckboxOperation(){this.dr = new FirefoxDriver();this.url = "https://kyfw.12306.cn/otn/lcxxcx/init";this.dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}public void action(){//打開12306查票頁面this.dr.get(this.url);//選擇出發(fā)城市this.dr.findElement(By.id("fromStationText")).click();this.dr.findElement(By.cssSelector("ul.popcitylist>li.ac_even.openLi.ac_odd[title='杭州']")).click();//選擇目的地城市this.dr.findElement(By.id("toStationText")).click();this.dr.findElement(By.cssSelector("ul.popcitylist>li.ac_even.openLi.ac_odd[title='廈門']")).click();this.dr.findElement(By.id("train_start_date")).click();//選擇日期。日期是一個(gè)iFrame。先找到這個(gè)iFrame,然后切換窗體到iFrame,再選擇日期并點(diǎn)擊WebElement calFrame = dr.findElement(By.xpath("/html/body/div[11]/iframe"));this.dr.switchTo().frame(calFrame);this.dr.findElement(By.xpath("/html/body/div[@class='WdateDiv WdateDiv2']/div[3]/table/tbody/tr/td[2]/table/tbody/tr[2]/td[last()]")).click();this.dr.switchTo().defaultContent();this.dr.findElement(By.id("_a_search_btn1")).click();//車次類型先全選上this.dr.findElement(By.id("span_station_train_code")).click();//取消其它和K字頭列車this.dr.findElement(By.xpath("//div[@id='sear-sel-bd']/div[1]/div[2]/ul/li/input[@value='QT']")).click();this.dr.findElement(By.xpath("//div[@id='sear-sel-bd']/div[1]/div[2]/ul/li/input[@value='K']")).click();//將全部checkbox的Label和是否選中打印出來List<WebElement> ccList = this.dr.findElements(By.xpath("//div[@id='sear-sel-bd']/div[1]/div[2]/ul/li"));for(int i = 0; i < ccList.size(); i ++){System.out.printf("%-20s isSelected:%-20s\n",ccList.get(i).findElement(By.tagName("label")).getText(),ccList.get(i).findElement(By.tagName("input")).isSelected());}this.dr.quit();}public static void main(String[] args) {CalendarCheckboxOperation cc = new CalendarCheckboxOperation();cc.action();} }12306頁面及程序執(zhí)行結(jié)果截圖
posted on 2017-06-13 18:31 mthoutai 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/mthoutai/p/7002847.html
總結(jié)
以上是生活随笔為你收集整理的Selenium_WebDriver操作iFrame日历框和复选框_Java的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【bzoj2693】jzptab 莫比
- 下一篇: [转] 深入浅出 妙用Javascrip