是否有明确的 Selenium 解决方案来使用 Java 在 Internet Explorer 中弹出模态弹出对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35946717/
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
Is there a definite Selenium solution to modal pop up dialogs in Internet Explorer with Java?
提问by Oscar R
I have searched many, many places for a solution to my problem, but haven't found it. I figured that by now, Selenium would have provided a straight forward and simple solution to handling modal windows/dialogs from Internet Explorer using Java.
我已经搜索了很多很多地方来解决我的问题,但还没有找到。我认为到目前为止,Selenium 会提供一种直接且简单的解决方案来使用 Java 处理来自 Internet Explorer 的模式窗口/对话框。
The web application that I am testing has the following characteristics:
我正在测试的 Web 应用程序具有以下特征:
- It is ONLY supported by Internet Explorer (no way around this)
- Main page accepts a userid and password, with a "Login" button
- Upon login and on page load, there is a pop up "Welcome - What's new" window with a checkbox to "Don't display this again" and an "OK" button to dismiss the window.
- I cannot do anything to the parent window until I dismiss the pop up window
- Right-click is disabled on the pop-up window (however, I can see the source code by opening the F12 tools before login and window pop-up)
- 它仅受 Internet Explorer 支持(无法解决此问题)
- 主页接受用户名和密码,带有“登录”按钮
- 在登录和页面加载时,会弹出一个“欢迎 - 新功能”窗口,其中包含一个“不再显示”复选框和一个“确定”按钮以关闭该窗口。
- 在关闭弹出窗口之前,我无法对父窗口执行任何操作
- 弹出窗口禁用右键单击(但是,我可以通过在登录和弹出窗口之前打开F12工具来查看源代码)
This is what I've tried:
这是我尝试过的:
getWindowHandles()
always returns 1 for the parent window, so this makesdriver.switchTo(handle)
not-applicable- It is not an alert, so
driver.switchTo().alert()
oraccept()
do not work findElement(By whatever)
will NOT find any elements in the pop up window (like the "OK" button or the checkbox, etc.)Robot
class is the only thing that I have seen work, where I can send keypresses to navigate to the "OK" button and click it to dismiss the window...
getWindowHandles()
总是为父窗口返回 1,所以这driver.switchTo(handle)
不适用- 这不是警报,因此
driver.switchTo().alert()
或accept()
不起作用 findElement(By whatever)
不会在弹出窗口中找到任何元素(如“确定”按钮或复选框等)Robot
class 是我唯一见过的工作,我可以在其中发送按键以导航到“确定”按钮并单击它以关闭窗口...
Here is my issue:
这是我的问题:
- Since there is a checkbox to "Don't show this again", there are users for which this modal pop up window will display and some for which it won't. I need to account for both cases
- I need to find a 100% sure way to know whether the pop up is displayed or not. If I have this information, I can make use of the Robot class (although "dirty") to perform actions on the pop up if needed
- I tried finding out if the parent window elements are enabled using
isEnabled()
, but even though items are not manually "clickable" while the modal pop up window is displayed,isEnabled()
always returns TRUE--so this does not work--is there a better way to check for this for the "blocked" elements in the background?
- 由于有一个“不再显示”复选框,因此有些用户会显示此模式弹出窗口,有些则不会。我需要考虑这两种情况
- 我需要找到一种 100% 确定的方法来知道是否显示弹出窗口。如果我有这些信息,我可以使用 Robot 类(尽管“脏”)在需要时对弹出窗口执行操作
- 我尝试找出是否使用 启用了父窗口元素
isEnabled()
,但是即使在显示模式弹出窗口时项目不是手动“可点击”,也isEnabled()
总是返回 TRUE——所以这不起作用——有没有更好的方法在后台检查“被阻止”的元素?
My questions:
我的问题:
- How do you check for the existence of a modal pop up that does not display 100% of the time? (on Internet Explorer 10, using Selenium with Java)
- Besides using Robot class, how do you interact with the actual Elements in a modal pop-up dialog (for example, dynamic Radio Buttons that don't always display the same options to the user)?
- 您如何检查是否存在未 100% 显示的模式弹出窗口?(在 Internet Explorer 10 上,使用 Selenium 和 Java)
- 除了使用 Robot 类之外,您如何与模式弹出对话框中的实际元素进行交互(例如,并不总是向用户显示相同选项的动态单选按钮)?
Thank you.
谢谢你。
回答by hfontanez
In my opinion, you should use WebDriverWait
with some expected condition. For example,
在我看来,您应该WebDriverWait
在某些预期条件下使用。例如,
WebDriverWait wait = new WebDriverWait(driver, 5); // sets timeout to 5 seconds
wait.until(...); // Use ExpectedCondition to set the condition you need to check for (i.e. element to be clickable, frame to be visible, etc.)
// Do your thing.
The until
method will return an object type relative to the function passed. For example, until(ExpectedConditions.elementToBeClickable(...));
will return a WebElement
object you can use to exert an action on (like clicking on it).
该until
方法将返回与传递的函数相关的对象类型。例如,until(ExpectedConditions.elementToBeClickable(...));
将返回一个WebElement
对象,您可以用来对其施加操作(例如单击它)。
Lastly, you should wrap those lines in a try/catch and handle the TimeoutException
the wait method will throw if the condition never arises.
最后,您应该将这些行包装在 try/catch 中,并处理TimeoutException
如果条件永远不会出现则等待方法将抛出的问题。
To summarize, structurally, your code should look something like this:
总而言之,在结构上,您的代码应如下所示:
// instantiate the WebDriver
...
int timeoutMax = 2; // The maximum number of seconds you wish to wait before timing out (let's assume 2 seconds is reasonable for your case)
try {
By checkboxLocator = By.id("checkboxID"); // Locate element by some criteria (id, css, xpath). Using by ID for illustration purposes only
By buttonLocator = By.id("buttonID"); // same as above
By popupLocator = By.id("frameid"); // same as above
WebDriverWait wait = new WebDriverWait(driver, timeoutMax);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(popupLocator)); // assuming it is an iframe
// The next lines will not be executed if the "Don't display this again" and clicking "OK" were clicked before (locating the frame will timeout because it not be visible)
WebElement checkbox = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
WebElement okBtn = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
checkbox.click();
okBtn.click();
driver.switchTo().defaultContent(); // Switch back to default window
} catch (TimeoutException exc) {
// Handle exception (i.e. log a warning) - This should be thrown as long as the modal dialog doesn't become visible
// If modal dialog ever becomes visible again, clicking on the checkbox and OK button will be executed again.
}
Something like this should work. Of course, this make some assumptions that might not be true for your case. However, if you use the right locating technique for your modal dialog, you should be able to:
像这样的事情应该有效。当然,这做出了一些可能不适用于您的案例的假设。但是,如果您为模态对话框使用正确的定位技术,您应该能够:
- Locate the modal window (use By class to locate it)
- Use
WebDriverWait
to setup your timeout conditions - Tell the driver to switch to it (if this times out, skip steps 3, 4, and 5)
- Locate the checkbox and OK buttons
- Click the checkbox and the OK button in that order
- Tell the driver to switch back to the main window
- Continue with your test
- 定位模态窗口(使用By类定位)
- 使用
WebDriverWait
设置您超时条件 - 告诉驱动程序切换到它(如果超时,请跳过步骤 3、4 和 5)
- 找到复选框和确定按钮
- 按顺序单击复选框和确定按钮
- 告诉司机切换回主窗口
- 继续你的测试
回答by phil o.O
Create an If statement in terms of a boolean variable to check for the existence of a modal pop up that does not display 100% of the time.
根据布尔变量创建一个 If 语句,以检查是否存在未 100% 显示的模式弹出窗口。
If the modal is html generated (which it should be, if it holds dynamic content), then try:
如果模态是 html 生成的(它应该是,如果它包含动态内容),然后尝试:
driver.switchTo().activeElement();
driver.switchTo().defaultContent();
driver.switchTo().activeElement();
driver.switchTo().defaultContent();
Also, you may have to insert a wait so that the html has time to generate.
此外,您可能需要插入一个等待,以便 html 有时间生成。
If the modal is a browser alert, then try:
如果模态是浏览器警报,请尝试:
alert.accept();
警报。接受();