Reputation: 576
Introduction
I want to automate some process and learn something new along the way. So far my scope is to be able to log into a website. For such thing I use the next code.
Code
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
System.setProperty("webdriver.chrome.driver","C:/Users/Jonathan/Desktop/Java/chromedriver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebElement element = null;
driver.get("https://somewebsite.com/");
//Representa un elemento HTML, interactuar con la pagina
driver.findElement(By.className("dialog_login_opener")).click();
WebElement userNameInputField = driver.findElement(By.id("mail_or_login_field"));
WebElement passwordInputField = driver.findElement(By.name("userData[password]"));
userNameInputField.sendKeys("[email protected]");
passwordInputField.sendKeys("123456");
element.submit();
//element.sendKeys("Hola Google!");
System.out.print(driver.getTitle());
Thread.sleep(5);
driver.quit();
}
Problem
So far I have been able to input the username right. The field used for such thing is:
<input type="text" name="userData[login]" tabindex="1" id="mail_or_login_field" class="input mail errtip tooltipstered" required="" placeholder="E-mail or Login">
so that explains what i have used before:
WebElement userNameInputField = driver.findElement(By.id("mail_or_login_field"));
but when it comes to input the password it doesn't do it. Instead throws at me this exception error I can not understand.
Password field:
<input type="password" name="userData[password]" tabindex="2" class="input pass1 errtip tooltipstered" required="" placeholder="Password">
Exception Error Log
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 72.0.3626.7 (efcef9a3ecda02..., userDataDir: C:\Users\Jonathan\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:55872}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 71.0.3578.98, webStorageEnabled: true}
Session ID: 8e80f9dcea262abb66d7888234704503
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
at seleniumtest.SeleniumTest.main(SeleniumTest.java:46)
C:\Users\Jonathan\AppData\Local\NetBeans\Cache\9.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Jonathan\AppData\Local\NetBeans\Cache\9.0\executor-snippets\run.xml:94: Java returned: 1
Upvotes: 1
Views: 795
Reputation: 5214
Just change the locator to By.xpath
like this:
WebElement passwordInputField = driver.findElement(By.xpath("//input[@type='password'"));
passwordInputField.sendKeys("123456");
Hope this helps you!
Upvotes: 1