-2

How to switch to parent tab to the new tab and open a new URL on Chrome Browser?

I want to access a new URL after opening the new tab so I have written the code as below but I am able to open the new tab and the system is opening the second URL in the parent window but not on the second tab?

getDriver().get("http://www.google.com");
String EsatwindowHandle = getDriver().getWindowHandle();

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T); 

Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_TAB);

getDriver().get("https://in.yahoo.com");
Robert
  • 7,394
  • 40
  • 45
  • 64
  • Why are you mixing Selenium and Robot? I don't think it makes for clearer code. Have you googled this? What did you find? – Robert Aug 28 '18 at 19:35
  • The above given references are to access a new tab by clicking a link from the parent tab and driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); //Switch between tabs using Ctrl + \t driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t"); Tried this but it's not working in Chrome and when I googled most of the people faced this issue in Chrome , so only I am using the Robot – Smart coder Aug 28 '18 at 19:43
  • any suggestion than above two refrences, I Tried all those and we can see the comments about the Chrome browser issue on these 2 refrences – Smart coder Aug 28 '18 at 19:45
  • 1
    Please don't use comments to clarify, [edit] your question instead. Comments may or may not be shown initially. They do appear in order of votes. You also cannot format code properly in comments. Make it easy to help you by putting everything in one place --- the question. You may want to take the [tour] of the site. – Robert Aug 28 '18 at 19:55

1 Answers1

0

Use WebDriver getWindowHandles() and switchTo() method as below:

WebDriver driver = getDriver();
driver.get("http://www.google.com");
String EsatwindowHandle = driver.getWindowHandle();         //get the window handler for main window

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T); 

Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_TAB);

//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

driver.get("https://in.yahoo.com");

To switch back to previous window(parent tab), write below code at the end

driver.switchTo().window(EsatwindowHandle);
Ali Azam
  • 2,047
  • 1
  • 16
  • 25
  • Hi Ali, logged in to Gmail and after sending an email if I want to open a new tab and Launch New URL do I need these 2 lines WebDriver driver = getDriver(); driver.get("http://www.google.com"); ? As I am already on the Gmail? – Smart coder Aug 29 '18 at 10:46
  • ANd after login to Yahoo How to switch back to parent tab i.e gmail tab, the above code is not working – Smart coder Aug 29 '18 at 10:46
  • @Smartcoder just comment the line `//driver.get("http://www.google.com");` if you be already on the Gmail. The last line will work to switch back to parent tab (i.e gmail tap) – Ali Azam Aug 29 '18 at 10:57