1

I want to open new tab in Firefox browser using get following is my code that I am using :

List<WebElement> url = industries.findElements(By.tagName("a"));

for (WebElement e : url) {
   String link = e.getAttribute("href");
   if (null == link)
     link = e.getAttribute("a");
   System.out.println(link);

  driver.get(link);
}

Here i am using get because i have already links in list so how i will open new tab in browser.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Ankan
  • 9
  • 2
  • 1
    Possible duplicate of [How to open a new tab using Selenium WebDriver with Java?](http://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver-with-java) – Madhan Jul 05 '16 at 06:39
  • [Check this](http://stackoverflow.com/a/19441842/3122133) – Madhan Jul 05 '16 at 06:40

2 Answers2

1

For open link in new tab you should try as below :-

import org.openqa.selenium.Keys;

String keys = Keys.chord(Keys.CONTROL,Keys.RETURN);

List<WebElement> url = industries.findElements(By.tagName("a"));

for (WebElement e : url) {
    e.sendKeys(keys);
}

Note :- If you are in Mac, need to replace Keys.CONTROL to Keys.COMMAND

Hope it will help you...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
1

use this command to open a new tab on the same browser

element.sendKeys(Keys.CONTROL + 't');

In your code you can user

url.sendKeys(Keys.CONTROL + 't');

Just use this command to open a new tab on your browser.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Devdutta Goyal
  • 985
  • 2
  • 11
  • 27