Selenium webdriver window switch

Source: Internet
Author: User
Tags xpath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.