简体   繁体   中英

How to switch multiple window without using page title in selenium webdriver using java?

I have 2 windows with same page title and same browser, used different session. After executing the automation script from first window it opens next window and execute the code. I am not able to switch back to the window.

I am using the below code :

Set<String> windowIterator = Driver.getWindowHandles();
Iterator Iter = windowIterator.iterator();
String Parent = (String) Iter.next();
String sub = (String) Iter.next();
Driver.switchTo().window(sub);`

I have tried with the updated script, here also facing issues to switch window.

package ABC;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;




public class UserChat {



    public static void main(String[] args) throws InterruptedException {


        WebDriver user_1 = new FirefoxDriver();

        user_1.manage().window().maximize();
        user_1.get("url");

        // login user_1
        user_1.findElement(By.xpath(".//*[@id='email']")).sendKeys("email1");
        user_1.findElement(By.xpath(".//*[@id='pass']")).sendKeys("pass");
        user_1.findElement(By.xpath("//label[@id='loginbutton']/input")).click();
        Thread.sleep(2000);


        WebDriver user_2 = new FirefoxDriver();
        user_2.manage().window().maximize();
        user_2.get("url");

        // login user_2 
        user_2.findElement(By.xpath(".//*[@id='email']")).sendKeys("email2");
        user_2.findElement(By.xpath(".//*[@id='pass']")).sendKeys("pass");
        user_2.findElement(By.xpath("//label[@id='loginbutton']/input")).click();
        Thread.sleep(2000);



        //click on Chat     
        WebDriverWait wait = new WebDriverWait(user_2, 60);
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/div[5]/div[1]/div/div/div[2]/div/div/a")));
        element.click();
        Thread.sleep(2000);

        //Search friend to message e.g. for user_1
        user_2.findElement(By.xpath("/html/body/div/div[5]/div[1]/div/div/div[2]/div/div/div/div/div/div[3]/div[2]/div/div/input")).sendKeys("My Friend User1");

        //Send message to friend
         WebElement webElement = user_2.findElement(By.xpath("/html/body/div/div[5]/div[1]/div/div/div[2]/div/div/div/div/div/div[3]/div[2]/div/div/input"));
         webElement.sendKeys(Keys.ENTER);
         Thread.sleep(3000);

         WebDriverWait wait2 = new WebDriverWait(user_2, 60);
        WebElement element2 = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/div[5]/div[1]/div/div/div[1]/div/div[1]/div[2]/div/div/div/div/div[4]/div[4]/div[1]/textarea")));
        element2.click();

         user_2.findElement(By.xpath("/html/body/div/div[5]/div[1]/div/div/div[1]/div/div[1]/div[2]/div/div/div/div/div[4]/div[4]/div[1]/textarea")).sendKeys("hi");
         element2.sendKeys(Keys.ENTER);
         Thread.sleep(2000);

    //   Actions action = new Actions(user_2);
      //   action.sendKeys(Keys.chord(Keys.CONTROL, "T")).build().perform();
      //   action.sendKeys(Keys.ALT,Keys.TAB).build().perform();


         Set<String> handles = user_2.getWindowHandles();
         int index = 0;
        if (handles.size() > index) {
             String handle = handles.toArray()[index].toString();
             user_2.switchTo().window(handle);
        }




    }


}

After done operation on second page use

driver.switchTo().defaultContent();

For you code you can use something like this :-

//Click your link
driver.findElement(By.xpath("xpath")).click();
//Get all the window handles in a set
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
//iterate through your windows
while (it.hasNext()){
String parent = it.next();
String newwin = it.next();
driver.switchTo().window(newwin);
//perform actions on new window
driver.close();
driver.switchTo().window(parent);

You are using two FirefoxDriver instances - in effect two different processes. Using web driver you can switch between the windows of a single driver instance. Once you maximise the second driver window, for switching back to the previous driver window - either you have to close the second window (which makes the OS to chose the earlier window to be on top), or minimise the second window or use OS specific library like Java Native Access to switch between them.

You are using two different driver instances.

If you want to interact with the first instance again just use the user_1 object. It's going to be sitting there until you close it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM