【问题标题】:How to open url on another tab at same browser using selenium webdriver?如何使用 selenium webdriver 在同一浏览器的另一个选项卡上打开 url?
【发布时间】:2015-06-14 20:45:54
【问题描述】:

我想通过单击添加帐户按钮在新标签上打开一个 URL。我正在使用框架,也找到了一些解决方案,但不明白如何应用它。

下面是我找到元素并将其返回以执行操作的代码。任何人都可以让我知道使用此代码集成在新选项卡中打开网址的代码吗?

private boolean operateWebDriver(String operation, String Locator,
            String value, String objectName) throws Exception {
        boolean testCaseStep = false;

        try {
            System.out.println("Operation execution in progress");
            WebElement temp = getElement(Locator, objectName);
            if (operation.equalsIgnoreCase("SendKey")) {
                temp.sendKeys(value);
            }
            Thread.sleep(1000);
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            if (operation.equalsIgnoreCase("Click")) {
                temp.click();

                //try to open account on another tab.
                String myWindowHandel= driver.getWindowHandle();
                driver.switchTo().window(myWindowHandel);

            }
            if (operation.equalsIgnoreCase("Verify")) {
                System.out.println("Verify--->" + temp);
                temp.isDisplayed();

            }
            testCaseStep = true;

        } catch (Exception e) {
            System.out.println("Exception occurred operateWebDriver"
                    + e.getMessage());

            // Take screenshot if any testcase is not working. 
            System.out.println("Taking Screen Shot");
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File("D:\\softs\\eclipse-jee-kepler-SR1-win32\\Workspace\\AutomationFramework\\Screenshot\\screenshot.jpeg")); 
        }

        return testCaseStep;
    }

    public WebElement getElement(String locator, String objectName)
            throws Exception {
        WebElement temp = null;
        System.out.println("Locator-->" + locator);
        if (locator.equalsIgnoreCase("id")) {
            temp = driver.findElement(By.id(objectName));

        } else if (locator.equalsIgnoreCase("xpath")) {
            temp = driver.findElement(By.xpath(objectName));
            System.out.println("xpath temp ----->" + temp);
        } else if (locator.equalsIgnoreCase("name")) {
            temp = driver.findElement(By.name(objectName));
        }
        return temp;

    }

}

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    您可以将此 if 条件添加到您的操作 WebDriver() 中

        if (operation.equalsIgnoreCase("newTab")) {
            System.out.println("newTab" + temp);
            Actions newTab = new Actions(driver);
            newTab.keyDown(Keys.COMMAND).click(temp).keyUp(Keys.COMMAND).build().perform();
    
            // Do your switch windows and other stuff you plan here after opening the new tab
    
        }
    

    例如,如果您登陆http://stackoverflow.com,并且您想在新选项卡中打开与您想要做的类似的问题按钮,您可以像这样调用该方法:

            //operateWebDriver(String operation, String Locator, String value, String objectName)
            operateWebDriver("newTab", "xpath", "", "//*[@id='nav-questions']");
    

    有关使用 selenium 打开新标签的更多信息:
    How to open a new tab using Selenium WebDriver and start the link?

    【讨论】:

    • 在 if 条件下我们使用 equalsIgnoreCase("newTab"),所以在我写测试用例的工作表中有一个操作列,其中写有“验证”、“单击”、“新标签”等,匹配后,它会自动执行操作。现在我想要的是当它单击“添加帐户”按钮时,它应该在同一浏览器的新选项卡上打开 URL,而不是新窗口。但是使用你给定的代码没有任何反应,所以你能根据我的场景告诉我代码吗?
    • 如果你在 Windows 上试试这个,“newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();” - 总之将答案中给出的方法中的COMMAND替换为CONTROL,请正确尝试。我已经测试了代码。它奏效了。
    • Nayank 谢谢它现在工作正常,新标签正在打开。有了这一行 newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();我切换到该窗口(打开的选项卡)但无法在其中恢复其他操作,就像我无法在字段中提供输入一样。那么,您有什么想法在打开窗口后,我们如何在其他选项卡上对新打开的窗口恢复其他操作?
    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 2015-03-25
    • 2018-02-01
    • 1970-01-01
    • 2016-08-12
    • 2014-02-18
    • 1970-01-01
    相关资源
    最近更新 更多