标签:cep ini interrupt ssi package com array ati read
selenium webdriver窗口切换,有时候在做自动化的时候需要打开很多很多的页面,
当在操作不同的页面的时候需要切换窗口,下面是如何切换到前后页面窗口的操作:
package test20161206; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class chuangkouqiehuan20161206 { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "C:\\Training\\Automation\\Software\\drivers\\chromedriver_win32\\chromedriver.exe"); WebDriver driver=new ChromeDriver(); driver.get("https://www.hao123.com/"); Thread.sleep(5000); driver.findElement(By.xpath("//*[@id=‘box-famoussite‘]/div/div[2]/span[2]/a")).click(); Thread.sleep(5000); System.out.println("进入58同城"); // String[] handles=new String[driver.getWindowHandles().size()]; // driver.getWindowHandles().toArray(handles); // driver.switchTo().window(handles[1]); for (String handle:driver.getWindowHandles()) { driver.switchTo().window(handle); } Thread.sleep(1000); driver.findElement(By.xpath("//*[@id=‘nav‘]/li[7]/a")).click(); Thread.sleep(5000); System.out.println("进入安居客"); // String[] handles1=new String[driver.getWindowHandles().size()]; // driver.getWindowHandles().toArray(handles1); // driver.switchTo().window(handles1[2]); for (String handle:driver.getWindowHandles()) { driver.switchTo().window(handle); } driver.findElement(By.xpath("//*[@id=‘header‘]/div[3]/div[2]/a[1]")).click(); Thread.sleep(5000); System.out.println("进入我要推广"); for (String handle:driver.getWindowHandles()) { driver.switchTo().window(handle); } driver.findElement(By.xpath("//*[@id=‘footer‘]/ul[1]/li[11]/a")).click(); Thread.sleep(5000); System.out.println("进入房贷计算器"); String[] handles=new String[driver.getWindowHandles().size()]; driver.getWindowHandles().toArray(handles); driver.switchTo().window(handles[2]); Thread.sleep(1000); driver.findElement(By.xpath("//*[@id=‘search-esf‘]")).sendKeys("上海浦东新区张江高科"); System.out.println("回到第三个窗口进行操作"); Thread.sleep(2000); Actions action=new Actions(driver); // action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).perform(); WebElement element=driver.findElement(By.xpath("//*[@id=‘search-esf‘]")); action.click(element).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform(); action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform(); action.click(element).keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform(); System.out.println("在输入框里Ctrl a,Ctrl c,Ctrl v"); } }
下面是却换到单个页面的前后页面,即:页面的前进和后退
driver.navigate().back();
点击当前页面后退按钮
driver.navigate().forward();
点击当前页面的前进按钮
driver.navigate().refresh();
点击当前页面的刷新按钮
标签:cep ini interrupt ssi package com array ati read
原文地址:http://www.cnblogs.com/111testing/p/6293092.html