4

I have used the code below trying to open same multiple window "Google". Kindly help me in editing this and explaining how to handle this .

driver.switchTo().window("gbar");//not sure how to use this

and below code tried in Selenium:

package Testing;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import junit.framework.*;

public class Float {

    public static void setUp() {

        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("https://www.google.com");
        driver.manage().window().maximize();
    }

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

        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("https://www.google.com");
        driver.manage().window().maximize();
        WebElement element = driver.findElement(By.name("q"));
        element.click();
        WebDriverWait wait = new WebDriverWait(driver, 80);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
        element.sendKeys("hi");
        element.clear();
        Thread.sleep(2000);
        element.sendKeys("hey");
        element.submit();
        setUp();

        driver.switchTo().window("gbar");// //* not sure how to use this *///
        WebElement element1 = driver.findElement(By.name("q"));
        element1.click();
        element1.sendKeys("hi");
        element1.clear();
        element1.sendKeys("hey");
        element1.submit();
        driver.quit();
    }
}
1
  • What actually do you want? Do you want to open a new Tab or new Window with same URL(in your case it is google.com? Commented Mar 1, 2016 at 11:46

3 Answers 3

4

You can get a handle to your window via driver.getWindowHandle()and you can switch to a window with driver.switchTo().window("handle");.

If you want to open a new window you can click on a link with target="_blank" on the website or execute JavaScript to open a new window. Then you'll find another handle in driver.getWindowHandles(). A possible way could be:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
List<String> knownHandles = new ArrayList<String>();
knownHandles.add(driver.getWindowHandle());
((JavascriptExecutor)driver).executeScript("window.open();");
// find the new handle. we are getting a set 
for (String handle : driver.getWindowHandles()) {
    if (!knownHandles.contains(handle)) {
        knownHandles.add(handle);
        break;
    }
}
String newHandle = knownHandles.get(knownHandles.size() -1 );
driver.switchTo().window(newHandle);
driver.get("https://www.google.com");

Another way is to inject the anchor and click it via JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

0
//Store the current window handle

String winHandleBefore = driver.getWindowHandle();

//Switch to new window opened

for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

// Perform the actions on new window

driver.manage().window().maximize();

//Close the new window, if that window no more required

driver.close();

//Switch back to original browser (first window)

driver.switchTo().window(winHandleBefore);

Comments

0

Here is a sample example handling multiple windows:

public class Mytesting {
 WebDriver driver = new FirefoxDriver();

 @Before
 public void beforetest() {
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
 }

@Test
  public void test () throws InterruptedException 
  {
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();

  // Get and store both window handles in array
  Set<String> AllWindowHandles = driver.getWindowHandles();
  String window1 = (String) AllWindowHandles.toArray()[0];
  System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
  String window2 = (String) AllWindowHandles.toArray()[1];
  System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);

  //Switch to window2(child window) and performing actions on it.
  driver.switchTo().window(window2);
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
  driver.findElement(By.xpath("//input[@value='Bike']")).click();
  driver.findElement(By.xpath("//input[@value='Car']")).click();
  driver.findElement(By.xpath("//input[@value='Boat']")).click();
  driver.findElement(By.xpath("//input[@value='male']")).click();
  Thread.sleep(5000);

  //Switch to window1(parent window) and performing actions on it.
  driver.switchTo().window(window1);
  driver.findElement(By.xpath("//option[@id='country6']")).click();
  driver.findElement(By.xpath("//input[@value='female']")).click();
  driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
  driver.switchTo().alert().accept();
  Thread.sleep(5000);

  //Once Again switch to window2(child window) and performing actions on it.
  driver.switchTo().window(window2);
  driver.findElement(By.xpath("//input[@name='fname']")).clear();
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("Name Changed");
  Thread.sleep(5000);
  driver.close();


  //Once Again switch to window1(parent window) and performing actions on it.
  driver.switchTo().window(window1);
  driver.findElement(By.xpath("//input[@value='male']")).click();
  Thread.sleep(5000);

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.