
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Open Link in New Tab of Chrome Browser using Selenium WebDriver
We can open a link in the new tab of Chrome browser using Selenium webdriver using the methods Keys.chord and sendKeys. The method Keys.chord is used to send multiple keys simultaneously as parameters.
To open a new tab, the Keys.CONTROL and Keys.ENTER are passed as parameters to the Keys.chord. Finally, the Keys.chord is passed as a parameter to the sendKeys.
Let us click on the Jobs links in a new tab, highlighted in the below image −
Syntax
String l = Keys.chord(Keys.CONTROL,Keys.ENTER); driver.findElement(By.xpath ("//a[@title='Job @ Tutorials Point']")).sendKeys(l);
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Keys; public class ElementLocator{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/index.htm"); //Keys.chord string String l = Keys.chord(Keys.CONTROL,Keys.ENTER); //open in a new tab driver.findElement(By.xpath ("//a[@title='Job @ Tutorials Point']")).sendKeys(l); } }
Output
Advertisements