Prateek Neelgund
Prateek Neelgund

Reputation: 11

Unable to open new tab in chrome, none of the solutions are working

I am working on Salesforce automation. There is a scenario which uses the one time sign on:

  1. I will log in to Salesforce application;
  2. Open a new tab and click on login with Salesforce account;
  3. Upload the excel and switch back to Salesforce tab.

I am able to login in Salesforce application but unable to open a new tab I have already tried below solution:

Solution 1:

Actions act = new Actions(driver);   
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();

Solution 2:

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

Solution 3:

String KeyString = Keys.CONTROL+"t";
driver.findElement(By.tagName("body")).sendKeys(KeyString);

Solution 4:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");

Has anyone faced issue? What could be the possible solution for this?

Upvotes: 0

Views: 186

Answers (1)

SteroidKing666
SteroidKing666

Reputation: 573

You can use JavascriptExecutor to switch to the new tab. Switching doesn't work automatically. So it's a two phase solution.

 ((JavascriptExecutor) driver).executeScript("window.open('http://example.com/','_blank');");

Above code will open example.com in a new tab.

Now you can switch using something like below:

Set<String> tab_handles = driver.getWindowHandles();
int number_of_tabs = tab_handles.size();
int new_tab_index = number_of_tabs-1;
driver.switchTo().window(tab_handles.toArray()[new_tab_index].toString());

Upvotes: 1

Related Questions