Home  >  Article  >  Java  >  Switching tabs using Selenium WebDriver and Java

Switching tabs using Selenium WebDriver and Java

王林
王林forward
2023-08-27 18:33:01788browse

We can switch tabs using Selenium. First, we have to open the link in a new tab. The Keys.chord method and sendKeys will be used. The Keys.chord method allows you to pass multiple keys at once. A key or string group is passed as a parameter to the method.

We pass Keys.CONTROL and Keys.ENTER as arguments to the Keys .Chord method. The entire string is then passed as a parameter to the sendKeys method. Finally, the sendKeys method must be applied to the link identified by the driver.findElement method.

Syntax

String clickl = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.xpath("//*[text()='Terms of Use']")). sendKeys(clickl);

Then save all open window IDs in an ArrayList and use the switchTo method to shift the driver focus to the new tab. Then pass the new tab's window ID as a parameter to the method.

Finally, after performing the task on the new tab, we can switch back to the parent window using the switchTo method and passing the parent window's window id as a parameter to the method.

Let’s switch between two tabs -

使用Selenium WebDriver和Java切换选项卡

The above is the detailed content of Switching tabs using Selenium WebDriver and Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete