Selenium webdriver window switch, sometimes in the automation of the need to open a lot of pages,
When you are working on a different page, you need to switch windows, and here's how to switch to the front and back page windows:
Packagetest20161206;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.Keys;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.chrome.ChromeDriver;Importorg.openqa.selenium.interactions.Actions; Public classchuangkouqiehuan20161206 { Public Static voidMain (string[] args)throwsinterruptedexception {//TODO auto-generated Method StubSystem.setproperty ("Webdriver.chrome.driver", "c:\\training\\automation\\software\\drivers\\chromedriver_win32\ \chromedriver.exe "); Webdriver Driver=NewChromedriver (); 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 ("Into the 58 of the same city"); //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 ("Access to the secure guest"); //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 ("Enter I want to promote"); 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 ("Enter the mortgage calculator"); String[] Handles=Newstring[driver.getwindowhandles (). Size (); Driver.getwindowhandles (). ToArray (handles); Driver.switchto (). Window (handles[2]); Thread.Sleep (1000); Driver.findelement (By.xpath ("//*[@id = ' search-esf ')"). SendKeys ("Shanghai Pudong New Area Zhangjiang Hi-Tech"); System.out.println ("Go back to the third window for action"); Thread.Sleep (2000); Actions Action=NewActions (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 ("In the input box Ctrl A,ctrl C,ctrl V"); }}
Here are the front and back pages of a single page, that is: the page's forward and backward
Driver.navigate (). back ();
Click the current page Back button
Driver.navigate (). Forward ();
Click the Forward button of the current page
Driver.navigate (). Refresh ();
Click the Refresh button on the current page
Selenium webdriver window switch