简体   繁体   English

如何在Selenium 3中打开新选项卡中的链接并关闭硒鼓中的选项卡?

[英]How to Open link in new tab and close tab in selenium 3?

Before tagging this as duplicate. 将其标记为重复之前。 Please read the Question. 请阅读问题。 i have seen many answers for this kind of question. 对于这种问题,我已经看到很多答案。 But none of them really worked. 但是他们都没有真正起作用。 The closest that worked is this code below by twobytehero.. 最接近的工作是下面的两个字节的代码。

Selenium 2: Open link in new tab and close tabs Selenium 2:在新标签页中打开链接并关闭标签页

But its opening a window instead of a tab and i am not able to control the delay. 但是它打开一个窗口而不是一个标签,我无法控制延迟。 I am using selenium 3. Using Selenium WebDriver with JAVA , I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). 我正在使用selenium3。将Selenium WebDriver与JAVA结合使用时,我试图自动化一项功能,在该功能中,我必须打开一个新选项卡,在那里进行一些操作,然后返回到上一个选项卡(父项)。 Whats the best possible way to do it with firefox ?? 用Firefox做到最好的办法是什么?

You can use approximately the same approach. 您可以使用大致相同的方法。 For opening new tab use shortcut: 要打开新标签页,请使用快捷方式:

Ctrl + T Ctrl + T

For navigating to previous tab: 导航到上一个选项卡:

Ctrl + Shift + Tab Ctrl + Shift + Tab

For closing current tab: 要关闭当前标签页:

Ctrl + W Ctrl + W

Code snipped for opening new tab can look like following: 删除用于打开新标签页的代码如下所示:

public static void openNewTab() {
    String openNewTabCombination = Keys.chord(Keys.CONTROL, "t");
    driver.get().findElement(By.tagName("body")).sendKeys(openNewTabCombination);
}

Also for moving to another tab with RemoteWebDriver you can use: 同样,要使用RemoteWebDriver移至另一个选项卡,您可以使用:

public static void moveToAnotherTab(RemoteWebDriver driver) {
    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM