Chapter XIV 6- how to switch the browser window

First, in an automated process, we need to operate from one window to another window do something, then how do we achieve this?

For example, we need to click on the figure "open new window" after a search in a new window.

Each window has a fixed handle each open window will have an id, we need to get selenium to find the id and then switch each handle (window)

 1 package switchto;
 2 
 3 import java.util.Set;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 import org.junit.jupiter.api.AfterEach;
 7 import org.junit.jupiter.api.BeforeEach;
 8 import org.junit.jupiter.api.Test;
 9 import org.openqa.selenium.By;
10 import org.openqa.selenium.WebDriver;
11 import org.openqa.selenium.WebElement;
12 import org.openqa.selenium.chrome.ChromeDriver;
13 
14 class SwitchWindow {
15     
16     WebDriver driver;
17     String url;
18     
19     @BeforeEach
20     void setUp() throws Exception {
21         driver = new ChromeDriver();
22         url = "file:///C:/Users/acer/Desktop/%E5%85%B6%E5%AE%83/PracticePage.html";
23         driver.manage().window().maximize();
24         driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
25         driver.get(url);
26     }
27 
28     @Test
29     void Test () throws InterruptedException {
 30  //         Get the main window handle (handle) 
31 is          String mainHandle = driver.getWindowHandle ();
 32          System.out.println ( "main window handle is:" + mainHandle);
 33 is  //         clicks "open a new window" button 
34 is          WebElement the openWindow = driver.findElement (By.id ( "OpenWindow" ));
 35          openWindow.click ();
 36          
37 [  //         get all window handle 
38 is          the Set <String> allHandle = driver.getWindowHandles ();
 39          
40  //         switch handle 
41 is         for (String handle: allHandle) {
 42 is              System.out.println ( "acquired handle is:" + handle);
 43 is              IF (! handle.equals (mainHandle)) {
 44 is                  . driver.switchTo () window (handle) ;
 45                  the Thread.sleep (3000 );
 46 is  //             in the new window Baidu page input box "Selenium" 
47                  WebElement seacherBox = driver.findElement (By.id ( "kW" ));
 48                  seacherBox.sendKeys ( "the Selenium" );
 49  //                 click on the search button 
50                  driver.findElement (By.id ( "su" .)) the click ();
51  //                 Close Window 
52 is                  driver.close ();
 53 is                  BREAK ;
 54 is              }
 55              
56 is  //             switch back to the main window 
57 is              . Driver.switchTo () window (mainHandle);
 58              driver.findElement (By.id ( "name") ) .sendKeys ( "Test" );
 59          }    
 60      }
 61 is      
62 is      @AfterEach
 63 is      void the tearDown () throws Exception {
 64          the Thread.sleep (2000 );
 65          driver.quit ();
 66     }
67 }

 

note:

1, driver.close () and driver.quit () the difference:

driver.close () only closes the browser window is currently operating.

driver.quit () to close all browser windows.

 

If you do not understand the small partners can be added to group "555 191 854" I asked, the group is small software industry partners together to learn from each other.

Content with coherence, unlabeled place to see the previous blog, which is a set of automated content on ava selenium +, starting with java foundation.

Welcome attention, please indicate the source.

Guess you like

Origin www.cnblogs.com/luohuasheng/p/11088189.html