无法使用Selenium在Element上输入字符串

时间:2019-01-01 21:04:02

标签: java selenium login automation

简介

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

代码

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

1 个答案:

答案 0 :(得分:1)

只需将定位器更改为UIColor.groupTableViewBackground ,如下所示:

By.xpath

希望这对您有帮助!