Home > Java > javaTutorial > body text

Switching tabs using Selenium WebDriver and Java

王林
Release: 2023-08-27 18:33:01
forward
817 people have browsed it

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);
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!