简体   繁体   English

使用Selenium Webdriver在新选项卡上打开页面后如何处理打开的页面?

[英]How to work on opened page after opening it on new tab using selenium webdriver?

I am able to switch on another tab at same browser but after opening that page, on another tab I am not able to perform the operation. 我可以在同一浏览器上打开另一个选项卡,但是打开该页面后,不能在另一个选项卡上执行该操作。

Current scenario held on: 目前的情况是:

  1. Give credential and login the account. 提供凭据并登录帐户。
  2. Click on email address or username through this it open a frame. 点击电子邮件地址或用户名,打开一个框架。
  3. From that frame click on "Add account" button. 在该框架中,单击“添加帐户”按钮。
  4. After click on "Add account" button I am successfully open the link on another tab. 单击“添加帐户”按钮后,我可以成功打开另一个选项卡上的链接。
  5. After opening tab, I am able to switch on that window. 打开标签页后,我可以打开该窗口。

But after switch on that window I want to give credential again on login page, but don't want to close the first opened tab on which account is loggedIn. 但是在打开该窗口后,我想在登录页面上再次提供凭据,但是不想关闭第一个打开的登录帐户的标签。

I am doing it in framework so please check the below code and according to this integrate your code and help me. 我正在框架中进行操作,因此请检查以下代码,并根据此代码集成您的代码并为我提供帮助。

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;

    }


}

Now I am able to work on opened URL after opening it on another tab. 现在,在另一个选项卡上打开URL之后,我就可以对其进行处理。 Here is the code which is working from my end. 这是从头开始起作用的代码。

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;
    }

which browser are you working on? 您正在使用哪个浏览器? firefox doesn't work on tab. Firefox在选项卡上不起作用。 try using chrome.. store current window handles and click the desired link to open new tab 尝试使用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));

            }

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

相关问题 在Selenium WebDriver中打开新标签后,如何使其在浏览器中显示为可见/活动标签? - After opening a new tab in Selenium WebDriver, how to make it the visible/active tab in my browser? 在Selenium Webdriver中使用Ctrl +单击组合打开新选项卡 - Opening a new tab using Ctrl + click combination in Selenium Webdriver 打开Selenium WebDriver后,如何控制新选项卡? - How do I control new tabs with Selenium WebDriver after they are opened? 使用 Selenium WebDriver 的新标签 - New Tab using Selenium WebDriver 如何使用Selenium WebDriver在新选项卡中打开网页 - How to open a web page in a new tab with Selenium WebDriver 如何通过将Selenium WebDriver与Java一起在同一浏览器中打开新标签页? - How to open a new tab in the same browser by using Selenium WebDriver with Java? 如何使用 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM