makeyser
makeyser

Reputation: 13

How can I get Selenium WebDriver (Java) to click this button?

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();

Could someone please help me with this?

Upvotes: 1

Views: 611

Answers (2)

Pradeep hebbar
Pradeep hebbar

Reputation: 2267

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

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.

Upvotes: 1

Nikola Andreev
Nikola Andreev

Reputation: 634

Did you try to inject JavaScript code into the browser?

driver.executeScript("setValue('agreetoTerms', 'Y')");

or

driver.executeScript("document.getElementsByName('buttonAction')[0].click()");

Upvotes: 0

Related Questions