Can't open new window/tab in chrome using webriver

3,012 views
Skip to first unread message

peter pala

unread,
May 18, 2016, 1:43:08 PM5/18/16
to Selenium Users
Hi All,

When I tried to open new tab/window in chrome browser,new url is open within same tab/window not new tab/window.
Below code work with Firefox perfectly.My chrome version is 28.0

 driver.get("https://www.google.com");
Actions act=new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform(); //Open new tab
driver.get("https://www.facebook.com"); // open facebook
act.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).keyUp(Keys.CONTROL).build().perform();
Please advise me to resolve this problem..
Thanks
Peter

Punkaaj Chavaan

unread,
May 20, 2016, 3:34:37 AM5/20/16
to Selenium Users
Hi Peter,

Your code wont open facebook.com in newly opened tab in chrome, below is the code that would work fine on both FF and chrome.

        System.setProperty("webdriver.chrome.driver",
                "D:\\bckup\\Documents\\Documents\\Selenium\\chrome driver\\2.21\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        String currWindow = driver.getWindowHandle();

        driver.get("https://www.google.com");
       
        Actions act = new Actions(driver);
        act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform(); // Open new tab
        Set<String> windowsList = driver.getWindowHandles();
        for (String str : windowsList) {
            if (!str.equals(currWindow)) {
                driver.switchTo().window(str);

            }
        }
        driver.get("https://www.facebook.com"); // open facebook



Regards,
Punkaaj
My Blog

Andreas Tolfsen

unread,
May 20, 2016, 8:04:41 AM5/20/16
to seleniu...@googlegroups.com
peter pala <kaln...@gmail.com> writes:

> When I tried to open new tab/window in chrome browser,new url is open
> within same tab/window not new tab/window. Below code work with
> Firefox perfectly.My chrome version is 28.0
>
> driver.get("https://www.google.com");
> Actions act=new Actions(driver);
> act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().p erform(); //Open new tab
> driver.get("https://www.facebook.com"); // open facebook
> act.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).keyUp(Keys.CONTROL).buil d().perform();

Generally I would not trust WebDriver to be able to trigger chrome-level
action sequences.

To open a new tab or window, you can execute `window.open`.

Fernando A. Rodriguez

unread,
May 23, 2016, 12:43:46 AM5/23/16
to seleniu...@googlegroups.com
Your code works well when I have only one instance running in front.
But it seems it doesn't work when you have another window being
displayed in front.
This scenario is very common when you run lot of tests in parallel using grid.

Regards
Fernando

punkaaj chavaan

unread,
May 23, 2016, 7:46:45 AM5/23/16
to seleniu...@googlegroups.com
Which browsers are you using to run this code in parallel?
Because, this code runs good on Chrome and FF but not on IE. While executing on IE, it opens the new tab but does not get facebook.com in newly opened tab, since both window handles (for tab1 and tab2) have same value. I am looking out for a workaround for this.

Regards,
Punkaaj
http://selenium-coding.blogspot.in

Fernando

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/D2UNieEuy8Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CALr8fN5FuJeex57NZ%2B%3D7uwqAfsYQeAKF2JuZb_Xci9qED7WA4g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Purushotham Karimbedu

unread,
May 27, 2016, 10:17:47 AM5/27/16
to seleniu...@googlegroups.com
Hi Peter pala, 

@Test public void Newtab() throws Exception {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://seleniumlearn.com"); {
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));

driver.get("http://facebook.com/chinna.in"); } }                            
       - See more at: http://www.seleniumlearn.com/selenium-commands#sthash.Hm1DqYRi.dpuf


OR


http://www.seleniumlearn.com/how-open-link-new-tab-or-window-and-switch-work-using-selenium



---
Thanks & Regards,
Purushotham Karimbedu,
Drupal Developer and QA Engineer,
Phone no: 09000109120.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages