1

I want to open two tabs in same window of google Chrome.I know this is common question. But none of the answers in them helped me to get this done. I'm using Chrome driver and java for it. I have tried to different code found on the internet and also in this website too. But none of them worked as I expected. It opened the window and load the website in first URL and then again load the second website in the same tab. It didn't even open a another tab.

In short form this is what I want to do.....

1) Open a chrome and load a page (no problem with this step)
2) After 10 seconds another tab should open and load a website in the same window
3) Switch to the second tab and work in there

I want to do this using Google Chrome and Java.

2
  • Are you going to interact then with that new tab?
    – Alexey R.
    Mar 28, 2018 at 6:35
  • Yes. second tab is the one I'm going to use while the first tab is kept open
    – Syrus
    Mar 28, 2018 at 6:39

1 Answer 1

3

use the below code

driver.get(enter the url);
((JavascriptExecutor)driver).executeScript("window.open()");
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");
4
  • first code works as I expected. But not the second code. In first code segment also you have to remove one line of these "driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");". If you could do I will accept this answer
    – Syrus
    Mar 28, 2018 at 9:46
  • @Joe thanks for your suggestion, i have updated the code
    – Vel Guru
    Mar 29, 2018 at 4:35
  • But still I can see the same code. Your first code segment works very well. Please remove the code segment. So that I can accept the answer immediatly
    – Syrus
    Mar 29, 2018 at 4:59
  • @Joe second code is removed
    – Vel Guru
    Mar 29, 2018 at 5:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.