0

Can anyone help me how to switch to new window after clicking on a hyperlink using selenium while doing automation . I have tried the following code but my test case is failing :

public void openView(){

        final WebElement visa=driver.findElement(By.id("pageContainer"));   
        timeOut(10000);
        final List<WebElement> images=visa.findElements(By.className("nametag"));
        String handlewindow = driver.getWindowHandle();
        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }
        images.get(0).findElement(By.className("info")).findElement(By.tagName("a")).click();;
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Anita Pandey
  • 5
  • 1
  • 6
  • What do you mean test case is failing?? in which line its failing?? and what is the exception?? – Saurabh Gaur Sep 09 '16 at 10:30
  • This [link](http://stackoverflow.com/questions/31603309/how-to-open-a-new-window-while-clicking-on-a-link-using-java) may help you out to get your answer. – ajay panchal Sep 09 '16 at 10:37
  • You said in a comment below that you're actually referring to a new tab, not a new window. Would you mind updating the question? – Hubert Grzeskowiak Sep 09 '16 at 12:18
  • @Anita can you please point out your steps what you are trying to do. From your question I can guess your steps like these: 1. You have one tab open in your browser? 2. Then from that tab you are trying to click a link? 3. Then you are trying to create a new Tab and trying to switch to that new Tab?? – Abir Khan Sep 11 '16 at 07:43

6 Answers6

0

This what we're using (Java):

public void switchToLatestPopupWindow()
{
    Iterator<String> handleIterator = this.driver.getWindowHandles().iterator();
    while (handleIterator.hasNext())
    {
        String handle = handleIterator.next();
        if (!handleIterator.hasNext())
        {
            this.driver.switchTo().window(handle);
        }
    }
}

public void resetToMainWindow()
{
    Iterator<String> handleIterator = this.driver.getWindowHandles().iterator();
    String parentHandle = null;
    while (handleIterator.hasNext())
    {
        String handle = handleIterator.next();
        if (parentHandle == null)
        {
            parentHandle = handle;
        } else if (!parentHandle.equals(handle))
        {
            this.driver.switchTo().window(handle);
            this.driver.close();
        }
    }
    this.driver.switchTo().window(parentHandle);
}
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
0

Assuming that the last line of code:

images.get(0).findElement(By.className("info")).findElement(By.tagName("a")).click()  

opens a new window, you are fetching the window handle of the parent window itself before the child window opens after click().

Secondly, here is the code for switching to a child window:

String handlewindow = driver.getWindowHandles().toArray()[1];
driver.switchTo().window(handlewindow);

The explanation is that driver.getWindowHandles() returns a Set of all open windows handles. Convert the Set to array and fetch the element at index 1 as child window is at 1, parent window is at 0.

0

Use this code picked up from another thread on Stackoverflow ...

WebElement link = driver.findElement(By.linkText("<your link text>")); 
Actions newwin = new Actions(driver);
newwin.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL)‌​.build().perform();

I checked this and it opens the link in a new tab. Then use this code to switch to the new tab ...

String handlewindow = driver.getWindowHandles().toArray()[1];
driver.switchTo().window(handlewindow);  
0

In the for loop we need to check if the current window handle is not equal to the one which is fetched by the loop

String handlewindow = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
    if (! handlewindow.equals (winHandle))
        driver.switchTo().window(winHandle);
}
manishgdev
  • 148
  • 3
  • 12
0
    Actions ac  = Actions(driverObj);
    String parentHandle = driverObj.getWindowHandle();
    WebElement we = driverObj.findElement(By.xpath("here is your xpath"));
    ac.keyDown(Keys.CONTROL).click(we).keyUp(Keys.CONTROL).build().perform();
    String currentHandle ="";
    Set<String> win  = ts.getDriver().getWindowHandles();   

    Iterator<String> it =  win.iterator();
    if(win.size() > 1){
        while(it.hasNext()){
            String handle = it.next();
            if (!handle.equalsIgnoreCase(parentHandle)){
                ts.getDriver().switchTo().window(handle);
                currentHandle = handle;
            }
        }
    }
    else{
        System.out.println("Unable to switch");
    }

Using above code you can open any link in new tab..

Ankit Gupta
  • 776
  • 5
  • 12
0

For switching between tabs you can use this methods:

    public void switchToNextTab() {
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(1));
    }

    public void switchToPreviousTab() {
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(0));
    }
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26