您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium Webdriver窗口切換
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2017/1/20 11:47:46 ] 推薦標簽:功能測試 Selenium

  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("上海浦東新區(qū)張江高科");
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();
  點擊當前頁面的刷新按鈕

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd