Ankan
Ankan

Reputation: 9

How to open link in new tab in selenium webdriver without using find by element

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.

Upvotes: 1

Views: 2016

Answers (2)

Devdutta Goyal
Devdutta Goyal

Reputation: 1135

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.

Upvotes: 1

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

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...:)

Upvotes: 1

Related Questions