0

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?

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Prateek Neelgund
  • 11
  • 1
  • 1
  • 6
  • Possible duplicate of [Opening link in the new tab and switching between tabs (Selenium Webdriver + Python)](https://stackoverflow.com/questions/46425797/opening-link-in-the-new-tab-and-switching-between-tabs-selenium-webdriver-pyt) – Ratmir Asanov Aug 07 '18 at 10:08
  • Nope, answer u mentioned is for MAC i am trying for windows. I have tried all possible solutions and unable to proceed further – Prateek Neelgund Aug 07 '18 at 10:12
  • The accepted answer is cross-platform. – Ratmir Asanov Aug 07 '18 at 10:14
  • I got the solution using javascript `String newURL =("URL"); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.open('"+newURL+"','_blank');");` But i am still wondering why other solutions are not supporting !! strange Thanks for your time @RatmirAsanov – Prateek Neelgund Aug 07 '18 at 10:23
  • Okey, good luck! – Ratmir Asanov Aug 07 '18 at 10:36
  • Possible duplicate of [How to open a new tab using Selenium WebDriver with Java?](https://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver-with-java) – Roberto Pegoraro Aug 07 '18 at 13:09

1 Answers1

1

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());
SteroidKing666
  • 553
  • 5
  • 13