-1

I am trying to navigate to other window. But firefox is opening new page in new tab, instead of new window. I check the checkbox check and uncheck both.

System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Selenium Purely\\geckodriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver(); //Launches FireFox browser with blank URL
driver.get("http://the-internet.herokuapp.com/windows");

Thread.sleep(4000);

String parentwindow = driver.getWindowHandle();

System.out.println("Parent window handle is  "+parentwindow);
System.out.println("Parent window title is  "+driver.getTitle());

driver.findElement(By.partialLinkText("Click Here")).click();

Set <String> s1 = driver.getWindowHandles();

Iterator <String> i1= s1.iterator();

while (i1.hasNext())                
{
    String childwindow = i1.next();

    System.out.println("Child window handle is  "+childwindow);
    System.out.println("Child window title is  "+driver.getTitle());

    if(parentwindow != childwindow)
    {
        driver.switchTo().window(childwindow);
        Thread.sleep(4000);

    }                   
}
f-CJ
  • 4,235
  • 2
  • 30
  • 28
Jafar
  • 27
  • 6
  • So you want to open a new window on `click here` click? – Kishan Patel Mar 21 '17 at 11:09
  • Yes. Its a free site for learning purpose. When i click on Click Here, Its not opening in new window - and opening in new tab. Manually when I do that, I can open in new window so as I have set the property accordingly. But not working out. – Jafar Mar 21 '17 at 11:15
  • Selenium Version 3.3.1 - Firefox 52 - – Jafar Mar 21 '17 at 11:16
  • Why you want to open in new window? Selenium allows switching multiples tabs. – Kishan Patel Mar 21 '17 at 11:16
  • 1
    There are few scenarios in my application where Help - Contact Us are displayed in new window. So, I need to follow it. – Jafar Mar 21 '17 at 11:25
  • [This answer](http://stackoverflow.com/a/17325883/2814308) might be of interest, or some of the other ones in that thread. – SantiBailors Mar 21 '17 at 11:35
  • if we open manually also it is opening new tab – SaiPawan Mar 21 '17 at 11:42
  • @SantiBailors - That answer is dealing with 2 different drivers. So it does not help me. – Jafar Mar 22 '17 at 09:52
  • I did not know if having 2 different instances of the same driver was OK or not for you, and I wrote "_or some of the other ones in that thread_" exactly because other answers in that page offer solutions that don't involve two instances of the driver, f.ex. [the one just below it](http://stackoverflow.com/a/28766108/2814308). You might want to take a peek at the other answers as well, if anything to add the info of why they don't help either. – SantiBailors Mar 22 '17 at 10:37

1 Answers1

0

Well, to work with Selenium you don't need to add the absolute file path of Firefox exe in your code as it will be added in your system PATH variable. So you can omit this line:

System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");

Now, to make Firefox to open URLs in new Windows there are some settings to be done. As far as your question is concerned How to open a new window instead of new tab here is the working code with IE:

package demo;


import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class TestAnyURLMain {

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


        System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
        WebDriver driver = new InternetExplorerDriver();
        driver.manage().window().maximize();
        driver.get("http://the-internet.herokuapp.com/windows");
        Thread.sleep(4000);
        String parentwindow = driver.getWindowHandle();

        System.out.println("Parent window handle is  "+parentwindow);
        System.out.println("Parent window title is  "+driver.getTitle());

        driver.findElement(By.partialLinkText("Click Here")).click();

        Set <String> s1 = driver.getWindowHandles();

        Iterator <String> i1= s1.iterator();

        while (i1.hasNext())

        {
            String childwindow = i1.next();

            System.out.println("Child window handle is  "+childwindow);
            System.out.println("Child window title is  "+driver.getTitle());

            if(parentwindow != childwindow)
            {
                driver.switchTo().window(childwindow);
                Thread.sleep(4000);

            }

        }

    }

}

Let me know if this helps you.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Yes. Pretty perfect with IE after updating Enable Protected Mode un-check. Now, I am trying same with firefox. I have added: System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); to make sure it pick it up correct profile which I am using too.. i.e. manually is working fine. Not via script. – Jafar Mar 22 '17 at 09:50
  • @Jafar this piece of code `System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");` is for the legacy Mozila Firefox users. As you are using gecko omit this line. – undetected Selenium Mar 22 '17 at 09:54
  • Dev - I just added that piece as the opening of new tab was not working. Just trial and error. However, it does not help me. – Jafar Mar 22 '17 at 10:14
  • 1
    @Jafar read my comment carefully. I asked you to remove that line. – undetected Selenium Mar 22 '17 at 10:25
  • Dev - I have removed that. But with firefox, adding that line or not adding that line - does not give me any diff. I read your comment correctly. I am just saying that - as it was not working, I try to add it and see the behavior. Does not give me any side effects as problem remain same with FF. – Jafar Mar 22 '17 at 10:34