简体   繁体   中英

Unable to login using selenium webdriver in Java

This is really weird but I suddenly cannot login using selenium. I was able to login with my code just a week ago and then I can't anymore. Here's the html code from the website:

 <div class="form-group"> <label for="loginUsername" style="width: 100%; font-weight: normal" aria-label="Username"> <input id="loginUsername" class="form-control" name="username" type="text" placeholder="Username" autocapitalize="off"> </label> </div> <div class="form-group"> <label for="loginPassword" style="width: 100%; font-weight: normal" aria-label="Password"> <input id="loginPassword" class="form-control" name="password" type="password" placeholder="Password" autocapitalize="off"> </label> </div> 

And here is my code before the incident:

driver.findElement(By.xpath("//*[@id=\"loginUsername\"]")).sendKeys("username");
driver.findElement(By.xpath("//*[@id=\"loginPassword\"]")).sendKeys("password");

It worked fine before. I even tried changing it up and it looked something like this:

WebElement username = driver.findElement(By.xpath("//*[@id=\"loginUsername\"]")); username.click(); username.clear(); ;username.sendKeys("username");
WebElement password = driver.findElement(By.xpath("//*[@id=\"loginPassword\"]")); password.click(); password.clear(); ;password.sendKeys("password");

I honestly don't know what is wrong with my code and I didn't get any error too. I also tried using "By.name()" or "By.id()" but it still didn't work so I'm really confused. Please help me, thank you!

edited Here's the full version of the code

 <div class="col-sm-4 col-sm-offset-4"> <form action="/login" method="post"> <div class="form-group title"> <img src="/img/RA_LOGO_BLUE.png" alt="Risk Assessment System Logo"> </div> <div class="form-group"> <label for="loginUsername" style="width: 100%; font-weight: normal" aria-label="Username"> <input id="loginUsername" class="form-control" name="username" type="text" placeholder="Username" autocapitalize="off"> </label> </div> <div class="form-group"> <label for="loginPassword" style="width: 100%; font-weight: normal" aria-label="Password"> <input id="loginPassword" class="form-control" name="password" type="password" placeholder="Password" autocapitalize="off"> </label> </div> <input id="loginBtn" class="btn" type="submit" value="Login"> </form> <br> <div class="text-left"> <span class="fakeLink" data-toggle="modal" data-target="#forgotPasswordModal">Forgot Password?</span><br> <span class="fakeLink" data-toggle="modal" data-target="#forgotUsernameModal">Forgot Username?</span> </div> </div> 

To send text to the username and password field you can use the following code block :

driver.findElement(By.xpath("//input[@id='loginUsername']")).sendKeys("username");
driver.findElement(By.xpath("//input[@id='loginPassword']")).sendKeys("password");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM