简体   繁体   English

使用 Selenium 在 Firefox 和 Chrome 中打开新标签页不起作用

[英]Open new tab in Firefox and Chrome with Selenium is not working

I read a lot of options related with the way to open new windows with Selenium.我阅读了很多与使用 Selenium 打开新窗口的方式相关的选项。 All the questions and answers are from a few years ago and maybe that's why they are not working to me.所有的问题和答案都是几年前的,也许这就是为什么它们对我不起作用。 And that's why I would like to open this question again.这就是为什么我想再次打开这个问题。

My first approach was using javascript action:我的第一种方法是使用 javascript 动作:

((JavascriptExecutor) getDriver()).executeScript("window.open('','NewWindow');");

My issue here is the different result in Firefox and Chrome.我的问题是 Firefox 和 Chrome 中的不同结果。 Firefox opens a new window and Chrome opens a new tab. Firefox 会打开一个新窗口,而 Chrome 会打开一个新选项卡。 This means that my test case is not working as expected if I executed in different browsers.这意味着如果我在不同的浏览器中执行,我的测试用例不会按预期工作。

After that I think about a different approach.之后,我想了一种不同的方法。 If I send the shortcut to open a new tab maybe both browsers will work with the same behavior.如果我发送快捷方式来打开一个新选项卡,也许两个浏览器都可以使用相同的行为。 And here started my nightmare.这开始了我的噩梦。 None of the next options open anything in the current Chrome and Firefox versions:在当前的 Chrome 和 Firefox 版本中,下一个选项都不会打开任何内容:

  1. Send keys concatenate the shortcut:发送键连接快捷方式:
     getDriver().findElement(By.xpath(".//body")).sendKeys(Keys.COMMAND+"T");
  2. Send keys multiple keys sequence:发送密钥多个密钥序列:
     getDriver().findElement(By.xpath(".//body")).sendKeys(Keys.COMMAND,"T");
  3. Send Keys chord发送键和弦
    getDriver().findElement(By.xpath(".//body")).sendKeys(Keys.chord(Keys.COMMAND + "T"));
  4. Using actions使用动作
    final Actions builder = new Actions(getDriver()); builder.keyDown(Keys.COMMAND).sendKeys("T").perform();

I'm thinking about try with the COMMAND key Down click on any link, but maybe there is an other easy way to open a new tab in different browsers.我正在考虑尝试使用 COMMAND 键向下单击任何链接,但也许还有其他简单的方法可以在不同的浏览器中打开新选项卡。 And this is my question, do you now an efficient way to open a new tab, not a new window, in different browsers with the same action?这是我的问题,你现在是否有一种有效的方式在不同的浏览器中以相同的操作打开一个新选项卡,而不是一个新窗口?

ADITIONAL INFORMATION附加信息

Selenium version -> 3.141.59硒版本 -> 3.141.59

Chrome version -> 79.0.3945.79 Chrome 版本 -> 79.0.3945.79

Firefox version -> 70.0.1火狐版 -> 70.0.1

Thank you in advance.先感谢您。

This may be help you:-这可能会帮助你:-

Using JavascriptExecutor :-使用JavascriptExecutor :-

  • Open new blank window:-打开新的空白窗口:-

((JavascriptExecutor)driver).executeScript("window.open('about:blank','_blank');");

  • Open new window with specific url:使用特定网址打开新窗口:

((JavascriptExecutor)driver).executeScript("window.open('http://www.yahoo.com','_blank');");

Using Robot class :-使用机器人类:-

Robot class in Selenium is used for simulating keyboard and mouse events. Selenium 中的机器人类用于模拟键盘和鼠标事件。 So, in order to open a new tab, we can simulate the keyboard event of pressing Control Key followed by 't' key of the keyboard.因此,为了打开一个新选项卡,我们可以模拟按下Control Key后跟键盘的't'键的键盘事件。 After the new tab gets opened, we need to switch focus to it otherwise the driver will try to perform the operation on the parent tab only.新选项卡打开后,我们需要将焦点切换到它,否则驱动程序将尝试仅在父选项卡上执行操作。 For switching focus, we will be using getWindowHandles() to get the handle of the new tab and then switch focus to it.为了切换焦点,我们将使用getWindowHandles()来获取新选项卡的句柄,然后将焦点切换到它。

//Use robot class to press Ctrl+t keys     
Robot robot = new Robot();                          
robot.keyPress(KeyEvent.VK_CONTROL); 
robot.keyPress(KeyEvent.VK_T); 
robot.keyRelease(KeyEvent.VK_CONTROL); 
robot.keyRelease(KeyEvent.VK_T);

//Implicit Wait
//driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
Thread.sleep(2000);

//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

//Launch URL in the new tab
driver.get("http://google.com");*/

Two approach of Robot Class to Open url in new tab using selenium Robot Class在新标签页中使用selenium打开url的两种方法

public class NewTab 
{

    public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver =  new FirefoxDriver();
        driver.get("http://www.facebook.com/"); 

        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_CONTROL); 
        r.keyPress(KeyEvent.VK_T); 
        r.keyRelease(KeyEvent.VK_CONTROL); 
        r.keyRelease(KeyEvent.VK_T);

        Set<String> tabs = (Set<String>)driver.getWindowHandles();

        for(String tab : tabs)
        {
            driver.switchTo().window(tab);
            System.out.println(driver.getTitle());
            if(driver.getTitle().contains("New Tab"))
                driver.get("http://www.google.com/");
        }
    }
}

Another way, without using the for loop另一种方式,不使用 for 循环

public class NewTab {

    public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver =  new FirefoxDriver();
        driver.get("http://www.google.com/");   

        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_CONTROL); 
        r.keyPress(KeyEvent.VK_T); 
        r.keyRelease(KeyEvent.VK_CONTROL); 
        r.keyRelease(KeyEvent.VK_T);

        String Base = driver.getWindowHandle();
                Set<String> tabs = (Set<String>)driver.getWindowHandles();

                tabs.remove(Base);
                driver.switchTo().window(tabs.toArray()[0].toString());
                driver.get("http://www.facebook.com/");
    }
}

Please try below code.请尝试以下代码。 I didn't use javascript.我没有使用 javascript。

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

As a user asked how I finally solved this, my solution was this:当用户问我最终如何解决这个问题时,我的解决方案是这样的:

// Save the current window reference
final String parentWindow = driver.getWindowHandle();
// Look for the element I would like to click
final WebElement elem = driver.findElement(By.xpath(xpath));
// Create an action to be performed
final Actions builder = new Actions(driver);
// The OSKEY is a global variable where depending on the OS is saved CMD or CTR
// With the special key chord, the program clicks on the element
builder.keyDown(OSKEY).click(elem).perform();

For me is working without browser problems.对我来说,工作没有浏览器问题。

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

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