Varesh Tuli
Varesh Tuli

Reputation: 45

Unable to click on a link after switching to other browser window in Selenium Webdriver java

I am try to click on a link on a newly opened browser window after clicking on a link on main window But webdriver is unable to find the element on the new opened window.

Below is my code which I am using to switch on the other window and then performing an action on it.

public void test() throws Exception{
    driver.findElement(By.xpath("//img[@alt='Calender']")).click();
    String Parent_Window =driver.getWindowHandle();

    for (String Child_Window : driver.getWindowHandles()) {
      driver.switchTo().window(Child_Window);
      driver.findElement(By.xpath("//div[@class='pbBody']//table[@class='list']//tr[@class='dataRow even first']//a")).click();
      // Webdriver is not able to click on the above link and it fails at this step
    }
    driver.switchTo().window(Parent_Window);
}

Please help me on this why wedrivber is unable to find the element on the new browser window after switching.Thanks!

Upvotes: 1

Views: 2980

Answers (4)

Golden Hash
Golden Hash

Reputation: 21

try this, i was having serious issue with windowhandles, but this one seems better.

String parentWindowHandler = driver.getWindowHandle(); 
        String subWindowHandler = null;

        Set<String> handles = driver.getWindowHandles();
        Iterator<String> iterator = handles.iterator();
        while (iterator.hasNext()){
            subWindowHandler = iterator.next();
        }
        driver.switchTo().window(subWindowHandler); 

        driver.findElement(By.xpath("")); //do something


        driver.switchTo().window(parentWindowHandler); 

Upvotes: 2

yojna
yojna

Reputation: 513

Don't find element inside for loop. Find element below for loop. (after for loop executed)

String parentWindow = webDriver.getWindowHandle();
Set<String> handles = webDriver.getWindowHandles();
    for (String windowHandle : handles) {
        if (!windowHandle.equals(parentWindow)) {
            webDriver.switchTo().window(windowHandle);
        }
    }

Upvotes: 1

Varesh Tuli
Varesh Tuli

Reputation: 45

Yes, I am able to run other scripts with the frame work but Even I dont know what's the problem is as I am able to print the title of that window but webdriver is unable to find any element on that window.

Upvotes: 1

ddavison
ddavison

Reputation: 29032

Your test is failing at your driver.findElement in your for loop because in the first iteration, it is selecting the first window and attempting to find that link. If that link doesn't exist in your parent window (not the popup window), then your test will fail at this step.

If there are only 2 windows open, then you should do:

public void test() throws Exception{
    driver.findElement(By.xpath("//img[@alt='Calender']")).click();
    String Parent_Window =driver.getWindowHandle();
    String Child_Window = driver.getWindowHandles().iterator().next();

    driver.switchTo().window(Child_Window);

    driver.findElement(By.xpath("//div[@class='pbBody']//table[@class='list']//tr[@class='dataRow even first']//a")).click();

    driver.switchTo().window(Parent_Window);
}

Alternatively, if you use the getting started with selenium framework your test would look like:

@Config(url="http://testsite.com")
public void Test extends AutomationTest {
    @Test
    public void test() {
        click(By.cssSelector("img[alt='Calender']"))
        .switchToWindow("Title of New Window")
        .click(By.cssSelector("div.pbBody table.list tr.dataRow.even.first a"))
        .switchToDefaultContent();
    }
}

Upvotes: 1

Related Questions