Selenium RC skips onclick() event after click on element - java

I've got problem I can't deal with. I've tried to search answer in google, tried few things but it doesn't work any way.
Here is the problem:
I'm testing a log in page, where I type in login, password and click "login" button. Everything is OK, I'm logging to the page... But before log in (after click "login" button) there should appear a prompt with some info and "OK" button, the prompt is onclick() event of "login" button. I have no idea why the prompt doesn't show up, and I really need this prompt (I have similar problem with prompt "Do you want to save changes" which also doesn't appear - I assumed that selenium.click(..,..) and webelement.click() somehow bypass onclick() events. Do you have any idea what to do to have onclick() events working properly? I'm using IE (I have to) and selenium webdriver.
Ps.:if I do same actions "manually" the prompts do appear so I don't think that its a mistake in javascript.
Ps2.: Help me please I'm trying to fix it for a 5 h...:(
button code:
<input id="loginForm:loginCmdTest" type="submit" onclick="showAlertAndSubmitForm ();clear_loginForm();" value="Login" name="loginForm:loginCmdTest">
java code:
selenium.type("id=loginForm:login", login);
selenium.type("id=loginForm:pass", pass);
selenium.click("id=loginForm:loginCmdTest");
Ps.3: ANYONE?? Any idea?

Plain guess: You are using old, depreceated Selenium RC. What happens if you transfer it to webdriver approach?
Sample code:
WebDriver driver = new FirefoxDriver();
driver.get("http://your-test-site.com");
driver.findElement(By.id("loginForm:login")).sendKeys("login");
driver.findElement(By.id("loginForm:pass")).sendKeys("pass");
driver.findElement(By.id("loginForm:loginCmdTest").click();

Related

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?
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.
Did you try to inject JavaScript code into the browser?
driver.executeScript("setValue('agreetoTerms', 'Y')");
or
driver.executeScript("document.getElementsByName('buttonAction')[0].click()");

button click() does not provide any response

I'm having issues with logging into specific website via HtmlUnit methods.
The site form's submit button looks like this:
<td>
<input type="button" value="Login!" onclick="encPass(UTM_STUDIO_ADMIN);" class="normalButton">
</td>
Mine code snippet:
final HtmlButtonInput submitLogin = form.getInputByValue("Login!");
HtmlPage returnPage = submitLogin.click();
System.out.println(returnPage.asText());
Yet, it prints the logging site with username and password fields fulfilled, that's all.
WebClient config:
wclient.getOptions().setPrintContentOnFailingStatusCode(false);
wclient.getOptions().setCssEnabled(false);
wclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
wclient.getOptions().setThrowExceptionOnScriptError(false);
wclient.getOptions().setUseInsecureSSL(true);
wclient.getOptions().setJavaScriptEnabled(false);
I've been already trying to log via my own added button, and played with ideas of waiting, enabling JS, redirecting etc., but I'm new in the topic so it does not guarantee I can uncheck any ideas as already tried.
Your button is of type button (not submit). In this case the browser renders only a button but the application developer is responsible for doing something if the user clicks the button. In you case the button will trigger some javascript.
But you have disabled Javscript by
wclient.getOptions().setJavaScriptEnabled(false);
Maybe your problem goes away if you enable javascript.
In general it is a good idea to start with the default setting when facing any problems. The defaults are set in a way to be as close as possible to the real browsers.

can't click on checkbox to uncheck it seem like it's hidden

the checkbox is checked be default and can't click on it to uncheck. here is my code but it came back as error saying element is not currently visible and so may not be interacted with. org.openqa.selenium.ElementNotVisibleException.
String checkboxXPath =("//input[contains(#type='checkbox',#name='key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal')]");
WebElement elementToClick = driver.findElement(By.xpath(checkboxXPath));
elementToClick.click();
Website code
<input type="checkbox" class="uwp_inputCheckBox"
name="key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal"
id="key_IT_CONFIG.ios.restriction.functionality.enable.camera"
value="true" dir="ltr" hierarchy="false" expand="true"
checkedval="true" uncheckedval="false"
onclick="checkboxChange('IT_CONFIG.ios.restriction.functionality.enable.camera')"
checked="checked">
whole code
whole code http://imageshack.com/a/img661/1720/SIi6Xj.png
I think you should use explicit wait until element get visible. Please check update code here and use it:
String checkboxXPath =("//input[contains(#type='checkbox',#name='key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal')]");
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(checkboxXPath)));
WebElement elementToClick = driver.findElement(By.xpath(checkboxXPath));
elementToClick.click();
I have a couple suggestions. I'm not sure why your XPath is so complex when you have an ID on the element you want to click. Try this...
driver.findElement(By.id("key_IT_CONFIG.ios.restriction.functionality.enable.camera"));
I'm kinda guessing that won't work. Looking at the HTML, I see the SPAN right above the element that you want to click and it has an onclick on it. I'm guessing that if you click that, it might trigger the click of the checkbox... so let's try that...
driver.findElement(By.cssSelector("span.uwp_checkBoxSpan.uwp_checkBoxChecked"));
You might need to check my spelling on the class names... I couldn't copy/paste since it's a picture.
Since Selenium works on Javascript I would suggest you to test the checkbox clicking thing manually by entering a Javvascript. Here are the step you need to follow:
Execute the test case manually up till where your script failed.
Goto browsers developer tools option->Console. Enter a javascript
command document.getElementById('key_IT_CONFIG.ios.restriction.functionality.enable.camera').click()
If this works then there is no reason why your code shouldn't work.

How to trigger Enter Key Function in Webdriver?

I ran in to a situation where sometimes clicking on a Submit button doesn't work in Selenium Webdriver. The same functionality could be obtained by pressing the Enter button.
Here, after entering the value in text box, I want Selenium WebDriver to press Enter key. How would I do that?
I'm using java
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("testing#netexam.com");
Thanks in advance!
Try this:
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("testing#netexam.com");
driver.findElement(By.id("email")).sendKeys(Keys.RETURN); //Send Enter Key

response.setHeader error on google chrome

This is a project on JSP.
1)I have this code where a button is pressed and the action is rendered to another page,which is a JSP page containing a pdf and the user is able to save it.
I had tried my code a month back and it worked fine,now when I try it,it doesnt work on chrome,works on mozilla though.
this is the line of code
response.setHeader("Content-Disposition", "inline; filename=Question.pdf");
2)My second problem is that,I have a reset button,this button resets the database.this function works fine on chrome but not on mozilla.I have no idea why it doesnt,there is no action taking place at all.
this is the line of code for that
<button type="button" onclick="reset.jsp">RESET</button>
For question 1):
What I know from the difference between "content-disposition", "attachment;filename=somefile.ext" and "content-disposition", "inline;filename=somefile.ext" is that in the inline case the browser will try to render it alone and if it can't it will prompt the user to download it. Maybe in your case you had an extension of Chrome that was capable of showing PDF files in the browser and now not. See Content-Disposition:What are the differences between "inline" and "attachment"?
As for question 2) :
This code onclick="reset.jsp" is not a valid construction. Onclick has to be a valid JavaScript function. See http://www.w3schools.com/jsref/event_onclick.asp. And it you case it not necessary to have it at all as you have an link inside the button which performs the actual action. I guess Chrome ignores this error and MOzilla not. Try removing onclick="reset.jsp" from the button.

Categories