java + selenium-- open multiple windows, and the window switching - Method a

At first to explain what is the handle, sometimes the browser window handle, also known as windows id, a better understanding of English words.

 

You open a browser, you open a tab, it generated a id, the id until you close the tab will disappear.

If you open more than one tab in a browser, there are more windows id, why do you close one tab, but does not affect the display of the other tab, this is the role of the id.

 

This article is to describe how to get a handle browser form or call the number for the driver to switch back to prepare for instance between multiple forms.

Let's take a look at the browser only open a tab, that is, when only a windows handle, print out the handle out.
================================================== ================================================== ======

 

Print results like this: CDwindow-e3655cc7-5cc1-42cc-a3d0-0d6339d1cfab


The id is every time you start changing, so, you print out the print and I certainly different.

 

Next, look at two tab, print out two windows to handle, use getWindowsHandles () method.

==============================================================================================================================

package rjcs;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.interactions.Actions;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import org.openqa.selenium.*;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.openqa.selenium.OutputType;

import com.thoughtworks.selenium.SeleneseTestNgHelper;

public class ckqh 
{
    
    public static void main(String[] args)
    
    {
         System.setProperty("webdriver.firefox.bin", "C: \\ Program Files (the x86) \\ \\ firefox.exe the Mozilla Firefox");     // set the installation path Firefox, preventing system can not find 
            
         FirefoxDriver Driver = new new FirefoxDriver () ;         // initializes FireFox browser instance, and open the browser 
         
        the try 
        { 
             driver.manage () window () maximize ();..          @ maximized window 
             
             the Thread.sleep ( 3000 );         
             
             . driver.manage () window () .maximize ();          // maximize the window 
             
             Thread.sleep ( 3000 ); 
        
             driver.get ( "https://www.baidu.com");                     //Open a Web site, a method              
        
             Thread.sleep ( 3000 );                   
                          
              driver.findElementByPartialLinkText ( "Homepage" ) .click (); 

             Thread.sleep ( 3000 );   
             
             
             
             System.out.println (driver.getWindowHandle ());     // Print this browser currently handles tabs 
             
             String a = driver.getWindowHandle (); 
             
             Thread.sleep ( 3000 ); 
             
             System.out.println (driver.getWindowHandles ());     // Print all browser tabs handle
                                                                // print out a list
             Thread.sleep (3000 ); 
             
             the Set <String> currenWindow driver.getWindowHandles = ();          // get all handles 
             System.out.println (currenWindow.size ());               // number of print handle (i.e., window) of 

             Thread. SLEEP ( 3000 );             
             
             . driver.switchTo () window (A);     // switch handle 
             
             the Thread.sleep ( 3000 ); 
             
        } the catch (Exception E) 
        { 
            e.printStackTrace (); 
        } the finally  
        { 
            driver.quit (); 
         
         }
   } 

}

 

 

Results of the:

5f9d93e2-496b-4402 {-A938-7968c83920db}
[{5f9d93e2-496b-4402-A938-7968c83920db}, {8beddbeb-0ce9-414c-8b83-2f8188da6bde}]
2

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12216060.html