We can switch tabs using Selenium. First, we have to open the link in a new tab. TheKeys.chord method andsendKeyswill 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 passKeys.CONTROLandKeys.ENTERas arguments to the Keys .Chord method. The entire string is then passed as a parameter to thesendKeysmethod. Finally, the sendKeys method must be applied to the link identified by thedriver.findElementmethod.
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 theswitchTomethod 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 theswitchTomethod and passing the parent window's window id as a parameter to the method.
Let’s switch between two tabs -
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!