

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How do I switch to the active tab in Selenium?
We can switch to the active tab in Selenium. The methods Keys.chord and sendKeys are used to open a new tab. More than one key can be passed at once with the Keys.chord method.
The Keys.CONTROL and Keys.ENTER combined are passed as parameters to the Keys.chord method. This is stored as a string and again passed as a parameter to the sendKeys method.
Syntax
String tb = Keys.chord(Keys.CONTROL,Keys.ENTER); driver.findElement(By.xpath("//*[text()='Company']")).sendKeys(tb);
All the window ids which are opened are stored in an array list. By default, the driver has the focus on the parent window. To switch the focus to the new tab, switchTo().window method is used.
The getWindowHandle method is used to hold the window id of the active tab and it is then passed as parameter to the switchTo().window method. The getWindowHandles method holds all the opened window ids.
Example
Code Implementation.
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 java.util.List; import java.util.ArrayList; public class SwitchActiveTab{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // wait of 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // Keys.Chord string String tb = Keys.chord(Keys.CONTROL,Keys.ENTER); driver.findElement(By.xpath("//*[text()='FAQ']")).sendKeys(tb); Thread.sleep(10000); // store window ids in array list ArrayList<String> wid = new ArrayList<String>(driver.getWindowHandles()); //switch to active tab driver.switchTo().window(wid.get(1)); System.out.println("Page title of active tab: " + driver.getTitle()); //switch to parent driver.switchTo().window(newTb.get(0)); System.out.println("Page title of parent window: " + driver.getTitle()); driver.quit(); } }
Output
- Related Questions & Answers
- C Sharp with Selenium - How to Switch one tab to another tab in Csharp Selenium?
- How to close active/current tab without closing the browser in Selenium-Python?
- How do I change focus to a new popup tab in Selenium?
- How to open new tab in same browser and switch between them using Selenium?
- How do I download Selenium RC?
- How do I use Selenium IDE?
- How to switch to frames in Selenium?
- How do I resolve the ElementNotInteractableException in Selenium WebDriver?
- How do I set the Selenium webdriver get timeout?
- How do I install selenium latest version?
- How do I use Selenium with Ruby?
- How do I open Chrome in selenium WebDriver?
- How do I get the src of an image in Selenium?
- How do I pass options to the Selenium Chrome driver using Python?
- How to open a new tab using Selenium WebDriver?