简体   繁体   English

如何使用Selenium WebDriver在同一浏览器的另一个选项卡上打开URL?

[英]How to open url on another tab at same browser using selenium webdriver?

I want to open a URL on new tab by clicking on button for add account. 我想通过单击添加帐户按钮在新选项卡上打开URL。 I am working with framework, also I've found some solution but don't understand how to apply it. 我正在使用框架,也找到了一些解决方案,但不了解如何应用它。

Below are the code where I've find the element and return it for performing the operations. 以下是我找到元素并返回以执行操作的代码。 Can any one let me know with integrate the code for opening the url in new tab, using this code? 谁能通过此代码让我知道在新选项卡中集成用于打开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;

    }

}

You can add this if condition into your operateWebDriver() 您可以将此条件添加到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

    }

For example if you landed on http://stackoverflow.com and you want to open Questions button in a new tab which is similar to what you want to do, you can call the method like this: 例如,如果您登陆http://stackoverflow.com,并且想要在新标签页中打开“问题”按钮,该标签与您要执行的操作类似,则可以调用如下方法:

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

For more info on opening a new tab using selenium: 有关使用硒打开新标签的更多信息:
How to open a new tab using Selenium WebDriver and start the link? 如何使用Selenium WebDriver打开新标签并启动链接?

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

相关问题 如何通过将Selenium WebDriver与Java一起在同一浏览器中打开新标签页? - How to open a new tab in the same browser by using Selenium WebDriver with Java? 如何在Selenium Webdriver中使用相同的会话打开浏览器? - How to open an browser using the same session in Selenium Webdriver? 如何使用 Selenium webdriver 打开特定浏览器 - How to open specific browser using Selenium webdriver 如何使用 Selenium WebDriver 在新标签页(chrome)中打开链接? - How to open a link in new tab (chrome) using Selenium WebDriver? 如何在 Java 中使用 Selenium WebDriver 打开一个新选项卡? - How to open a new tab using Selenium WebDriver in Java? 如何使用Selenium Webdriver打开新的Chrome标签页? - How to open a new chrome tab using selenium webdriver? 如何单击新标签中的打开链接以使用 selenium webdriver 在主标签中显示的新标签中打开链接 - How to click on open link in new tab to open the link in new tab that appears in main tab using selenium webdriver 如何使用带有 Java 的 Selenium WebDriver 切换到另一个选项卡 - How to switch to another Tab using Selenium WebDriver with Java 使用Java而不是javascript,在默认浏览器的同一标签中打开URL - using java not javascript, open URL in same tab of default browser 如何使用 Selenium 在 Chrome 浏览器上打开新标签页 - How to open new tab on Chrome browser using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM