【问题标题】:How to work on opened page after opening it on new tab using selenium webdriver?使用 selenium webdriver 在新选项卡上打开页面后如何处理打开的页面?
【发布时间】:2015-08-31 21:07:33
【问题描述】:

我可以在同一浏览器中打开另一个标签页,但打开该页面后,在另一个标签页上我无法执行该操作。

当前情景举行:

  1. 提供凭据并登录帐户。
  2. 点击电子邮件地址或用户名通过它打开一个框架。
  3. 在该框架中单击“添加帐户”按钮。
  4. 点击“添加帐户”按钮后,我成功打开了另一个选项卡上的链接。
  5. 打开标签后,我可以打开那个窗口。

但打开该窗口后,我想在登录页面上再次提供凭据,但不想关闭帐户登录的第一个打开的选项卡。

我是在框架中做的,所以请检查下面的代码,并据此整合你的代码并帮助我。

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(10, TimeUnit.SECONDS);
            if (operation.equalsIgnoreCase("Click")) {
                temp.click();

            }
            if (operation.equalsIgnoreCase("newTab")) {
                System.out.println("newTab" + temp);
                Actions newTab = new Actions(driver);
                newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();
               newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
            }
            if (operation.equalsIgnoreCase("Verify")) {
                System.out.println("Verify--->" + temp);
                temp.isDisplayed();
            }

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

            //Taking snapshot when test case fail.

            System.out.println("Taking snapshot");

            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
            FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));

        }

        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));
            System.out.println("name temp ----->" + temp);
        } 
        return temp;

    }


}

【问题讨论】:

  • 你想在哪里继续,是指在主窗口还是在新打开的窗口?
  • @SarithaG 我想在同一个窗口但在不同的选项卡上工作,比如在新选项卡上打开一个链接后,我的测试操作应该可以工作。
  • 是的..使用那个链接。那会奏效的。

标签: java selenium selenium-webdriver


【解决方案1】:

现在我可以在另一个选项卡上打开 URL 后处理打开的 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(10, TimeUnit.SECONDS);
            if (operation.equalsIgnoreCase("Click")) {
                temp.click();

            }
            if (operation.equalsIgnoreCase("newTab")) {
                System.out.println("newTab" + temp);

                // Open link in new tab.
                Actions newTab = new Actions(driver);
                newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();

               //After Opening link in new tab display that window.
                newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();

               // Get the number of tab opened in browser.
                ArrayList<String> newTab1 = new ArrayList<String>(driver.getWindowHandles());
                // change focus to new tab
                driver.switchTo().window(newTab1.get(0));

                // Now perform the operation here.
                if (operation.equalsIgnoreCase("SendKey")) {
                    temp.sendKeys(value);
                }
                Thread.sleep(1000);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                if (operation.equalsIgnoreCase("Click")) {
                    temp.click();

                }

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

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

            //Taking snapshot when test case fail.

            System.out.println("Taking snapshot");

            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
            FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));

        }

        return testCaseStep;
    }

【讨论】:

  • 很高兴你得到了适合你的东西,但你很幸运使用getWindowHandles()返回的列表中的第一个值。没有保证窗口句柄的顺序。添加逻辑以找到正确的窗口句柄留给读者作为练习;示例代码可在网络上的许多地方找到。
  • @JimEvans 感谢您的反馈,能否提供参考网址?
  • 你得到了多少个窗口句柄,你是说在新标签页中打开新页面,如果是标签页,那么只有1个窗口句柄对吗?
  • @skumar 是的,目前它只处理 1 个窗口。
【解决方案2】:

您正在使用哪种浏览器? Firefox 在选项卡上不起作用。 尝试使用 chrome.. 存储当前窗口句柄并单击所需的链接以打开新标签

//current window - before opening link on new tab    
String winHandleBefore = driver.getWindowHandle();  
  //write code to open new link
    tabs2 = new ArrayList<String> (driver.getWindowHandles());

        for( int k= 0 ; k< tabs2.size(); k++  ){
                for(String winHandle : driver.getWindowHandles()){
             driver.switchTo().window(tabs2.get(k));

            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-10
    • 2021-10-15
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2013-02-13
    • 2017-01-05
    • 2015-07-26
    相关资源
    最近更新 更多