如何使用 Java 处理 Selenium WebDriver 中的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19403949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:52:35  来源:igfitidea点击:

How to handle Pop-up in Selenium WebDriver using Java

javaselenium-webdriverpopuppopupwindow

提问by Niyati

I want to handle sign-in part in rediff.com, but the below code doesn't work for that:

我想在 rediff.com 中处理登录部分,但下面的代码对此不起作用:

driver.get("http://www.rediff.com/");
WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
sign.click();
String myWindowHandle = driver.getWindowHandle();
driver.switchTo().window(myWindowHandle);
WebElement email_id= driver.findElement(By.xpath("//*[@id='signin_info']/a[1]"));
email_id.sendKeys("hi");

If myWindowHandleis not the correct string, then let me know how to get the pop-up Window name, because I can't find the name of the pop-up window.

如果myWindowHandle不是正确的字符串,请告诉我如何获取弹出窗口名称,因为我找不到弹出窗口的名称。

采纳答案by LINGS

To switch to a popup window, you need to use getWindowHandles()and iterate through them.

要切换到弹出窗口,您需要使用getWindowHandles()并遍历它们。

In your code you are using getWindowHandle()which will give you the parent window itself.

在您正在使用的代码中getWindowHandle(),它将为您提供父窗口本身。

String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
    subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window

// Now you are in the popup window, perform necessary actions here

driver.switchTo().window(parentWindowHandler);  // switch back to parent window

回答by Hemanth

Do not make the situation complex. Use IDif they are available.

不要把情况复杂化。ID如果可用,请使用它们。

driver.get("http://www.rediff.com");
WebElement sign = driver.findElement(By.linkText("Sign in"));
sign.click();
WebElement email_id= driver.findElement(By.id("c_uname"));
email_id.sendKeys("hi");

回答by Niyati

I found the solution for the above program, which had the goal of signing in to http://rediff.com

我找到了上述程序的解决方案,其目标是登录http://rediff.com

public class Handle_popupNAlert
{
    public static void main(String[] args ) throws InterruptedException
    {
        WebDriver driver= new FirefoxDriver(); 
        driver.get("http://www.rediff.com/");
        WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
        sign.click();

        Set<String> windowId = driver.getWindowHandles();    // get  window id of current window
        Iterator<String> itererator = windowId.iterator();   

        String mainWinID = itererator.next();
        String  newAdwinID = itererator.next();

        driver.switchTo().window(newAdwinID);
        System.out.println(driver.getTitle());
        Thread.sleep(3000);
        driver.close();

        driver.switchTo().window(mainWinID);
        System.out.println(driver.getTitle());
        Thread.sleep(2000);

        WebElement email_id= driver.findElement(By.xpath("//*[@id='c_uname']"));
        email_id.sendKeys("hi");
        Thread.sleep(5000);

        driver.close();
        driver.quit();
    }  
}

回答by Satheesh kumar

       //get the main handle and remove it
       //whatever remains is the child pop up window handle

       String mainHandle = driver.getWindowHandle();
       Set<String> allHandles = driver.getWindowHandles();
       Iterator<String> iter = allHandles.iterator();
       allHandles.remove(mainHandle);
       String childHandle=iter.next();

回答by ER.swatantra

You can handle popup window or alert box:

您可以处理弹出窗口或警报框:

Alert alert = driver.switchTo().alert();
alert.accept();

You can also decline the alert box:

您还可以拒绝警告框:

Alert alert = driver.switchTo().alert();
alert().dismiss();

回答by Sandip

When the toastr message poped up on the screen of firefox. the below tag was displayed in fire bug.

当 Firefox 的屏幕上弹出 toastr 消息时。下面的标签显示在 fire bug 中。

<div class="toast-message">Invalid Credentials, Please check Password</div>.

I took the screenshot at that time. And did the below changes in selenium java code.

我当时截图了。并在 selenium java 代码中做了以下更改。

String alertText = "";
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("toast-message")));
WebElement toast1 = driver.findElement(By.className("toast-message"));  
alertText = toast1.getText();
System.out.println( alertText);

And my issue of toastr popup got resolved.

我的 toastr 弹出问题得到了解决。

回答by ankush deol

public void Test(){

     WebElement sign = fc.findElement(By.xpath(".//*[@id='login-scroll']/a"));
        sign.click();
        WebElement LoginAsGuest=fc.findElement(By.xpath(".//*[@id='guest-login-option']"));
        LoginAsGuest.click();
        WebElement email_id= fc.findElement(By.xpath(".//*[@id='guestemail']"));
        email_id.sendKeys("[email protected]");
        WebElement ContinueButton=fc.findElement(By.xpath(".//*[@id='contibutton']"));
        ContinueButton.click();

}

回答by Manasis Mishra

You can use the below code inside your code when you get any web browser pop-up alert message box.

当您收到任何 Web 浏览器弹出警报消息框时,您可以在代码中使用以下代码。

// Accepts (Click on OK) Chrome Alert Browser for RESET button.

Alert alertOK = driver.switchTo().alert();
alertOK.accept();



//Rejects (Click on Cancel) Chrome Browser Alert for RESET button.

Alert alertCancel = driver.switchTo().alert();
alertCancel.dismiss();

回答by user3150673

String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();

subWindowHandler = iterator.next();

driver.switchTo().window(subWindowHandler); // switch to popup window

// Now you are in the popup window, perform necessary actions here

driver.switchTo().window(parentWindowHandler);  // switch back to parent window