My code works with Chrome Driver. It also works with the HtmlUnit driver for the username field. And it also works for the password field if I don't enable JavaScript for the driver. However, For some reason it has an issue with the password field if JavaScript is enabled. My only thought for why it would not work with the password field is because there is JavaScript keypress methods set on the password field.
Source code below for the webpage.
<input type="password" name="pw" id="password" value="" size="35" autocomplete="off" maxlength="128" class="">
<script>
$(document).ready(function(){
$('#loginform').submit(function(e){
e.stopPropagation();
});
$('#password').keypress(function(e){
if(e.keyCode == 13){
$('#loginform').submit();
}
});
$('#password').keypress(function(e){
var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
if(w >=65 && w <= 122){//only alter when ASCII characters are typed
var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
if(c){
$('#capslockwarning').slideDown();
} else {
$('#capslockwarning').slideUp();
}
}
});
});
Selenium code
WebDriver driver = new HtmlUnitDriver(true);
driver.get("example.com");
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.xpath("//*[@id=\"password\"]"));
username.sendKeys("username");
password.sendKeys("password"+Keys.ENTER);
The username works. The password for some reason does not. Please help.
Thanks