java - 如何使用selenium webdriver在同一浏览器的另一个选项卡上打开网址?

java - 如何使用selenium webdriver在同一浏览器的另一个选项卡上打开网址?,第1张

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

下面是我找到元素并返回它以执行操作的代码。任何人都可以通过使用此代码集成在新标签页中打开网址的代码来告诉我吗?

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;

    }

}

最佳答案:

1 个答案:

答案 0 :(得分:0)

您可以将此if条件添加到您的operateWebDriver()

    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?

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复