How to Open New Browser Tab in Selenuim
Answer − We can open a new browser tab in Selenium webdriver. The methods – Keys.chord and sendKeys are required to achieve this task. The Keys.chord method is used to send multiple keys at once.
We shall pass Keys.CONTROL and Keys.ENTER as parameters to this method. The complete string is again passed as a parameter to the sendKeys. Finally, the method sendKeys shall be applied on the link which we want to open in a new tab
Syntax
String l = Keys.chord(Keys.CONTROL,Keys.ENTER); driver.findElement(By.xpath("//*[text()='Links']")). sendKeys(l);
Code Implementation
package RK1.Building_a_selenium_project;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
public class OpenNextTab {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "F:\\Work Environment\\MyProject\\QA_Round\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// wait of 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
// Keys.Chord string
String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
// open new tab
driver.findElement(By.xpath("//*[text()='Terms of Use']")).sendKeys(l);
driver.quit();
}
}
8 COMMENTS
Please provide me with more details on the topic
Sure, what is your query?
Thank you for your articles. They are very helpful to me. May I ask you a question?
Thankyou for your feedback, yes you can ask the question…
Hi there! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no backup. Do you have any solutions to protect against hackers?
Greetings from California! I’m bored at work so I decided to check out your blog on my iphone during lunch break. I love the information you provide here and can’t wait to take a look when I get home. I’m shocked at how fast your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyhow, awesome site!
Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thanks
My spouse and I stumbled over here coming from a different web address and thought I might check things out. I like what I see so i am just following you. Look forward to checking out your web page for a second time.