1

介绍

我想自动化一些过程并在此过程中学习一些新东西。到目前为止,我的范围是能够登录到一个网站。对于这样的事情,我使用下一个代码。

代码

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@gmail.com");
        passwordInputField.sendKeys("123456");


        element.submit();

        //element.sendKeys("Hola Google!");

        System.out.print(driver.getTitle());

        Thread.sleep(5);

        driver.quit();  

    }

问题

到目前为止,我已经能够正确输入用户名。用于此类事情的字段是:

<input type="text" name="userData[login]" tabindex="1" id="mail_or_login_field" class="input mail errtip tooltipstered" required="" placeholder="E-mail or Login">

这样就解释了我以前使用过的内容:

WebElement userNameInputField = driver.findElement(By.id("mail_or_login_field"));

但是当涉及到输入密码时,它不会这样做。相反,我无法理解这个异常错误。

密码字段:

<input type="password" name="userData[password]" tabindex="2" class="input pass1 errtip tooltipstered" required="" placeholder="Password">

异常错误日志

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
4

1 回答 1

1

只需将定位器更改为By.xpath如下所示:

WebElement passwordInputField = driver.findElement(By.xpath("//input[@type='password'"));
passwordInputField.sendKeys("123456");

希望这对你有帮助!

于 2019-01-02T10:15:08.790 回答