如何获得Selenium WebDriver(Java)单击此按钮? [英] How can I get Selenium WebDriver (Java) to click this button?

查看:432
本文介绍了如何获得Selenium WebDriver(Java)单击此按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Selenium WebDriver自动执行测试用例.到目前为止(使用Selenium和Java),我可以打开测试网站,输入用户名和密码,然后登录.但是,登录后,将用户重定向到带有安全警告的屏幕,该警告必须为在他们可以访问实际网站之前被接受.为此,他们必须单击名为我同意"的按钮.我无法让Selenium单击该按钮,没有它,我将无法访问该网站的其余部分来实现自动化.这是按钮的HTML:

I am new at using Selenium WebDriver to automate test cases. So far (using Selenium and Java), I am able to open the testing website, enter the username and password, and log in. After logging in, however, the user is re-directed to a screen with a security warning that must be accepted before they can access the actual website. To do this, they must click on a button called "I Agree". I can't get Selenium to click the button and, without it, I can't get to the rest of the site to automate. Here is the HTML for the button:

<form name="landingHandlerSF" method="post" action="/apps/bap/secLandingHandler.do">
<input name="userAgreedTerms" value="" type="hidden">
<input name="submit" value="landing" type="hidden">
<input name="buttonAction" value="I Agree" onclick="setValue('agreetoTerms', 'Y')" type="submit">
</form>

这是我尝试过的代码(不起作用):

Here is the code I have tried (which doesn't work):

WebElement button = driver.findElement(By.name("buttonAction"));
button.click();

有人可以帮我吗?

推荐答案

根据我的理解,页面正在重定向到新的html页面,但驱动程序将指向父页面(以您的情况为登录页面),因此您可能必须切换到child window以单击我同意按钮.

As per my understanding, page is redirecting to new html page but driver will be pointing to parent page(Login page in your case) , so you may have to switch to child window in order to click on I Agree button.

以下代码将驱动程序从当前窗口(即登录窗口)切换到新窗口(即安全警告).单击我同意后,将关闭安全警告,驱动程序将自动切换回

The following code will switch the driver away from the current window (ie, the login window) to the new window (ie, the security warning). After clicking I Agree that security warning will be closed and the driver will switch back automatically

String thisWindow = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();
for (String windowHandle : windowHandles) {
    if (!windowHandle.contains(thisWindow)) {
        driver.switchTo().window(windowHandle);
    }
}

如果它不导航到新页面,则它必须位于某个iframes下,在这种情况下,您可能需要切换到框架并单击按钮.

If it is not navigating to new page then it must be under some iframes , in that case you may need to switch to frame and click the button.

希望这会起作用. 试试这个,让我知道发生了什么.

Hope this will work. Try this and let me know what happened.

这篇关于如何获得Selenium WebDriver(Java)单击此按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆