简体   繁体   English

硒用户名密码错误

[英]Selenium Username Password Code Error

I am writing a code that enters the username and password into a URL and submits that page. 我正在编写一个将用户名和密码输入URL并提交该页面的代码。 but I keep getting this error 但我不断收到这个错误

 "Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: 
  Unable to locate element: {"method":"name","selector":"username"}"

Below is the code 下面是代码

package org.openqa.selenium.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;

        // Check that we're on the right page.
        if (!"Outreach Configuration".equals(driver.getTitle())) {
            // Alternatively, we could navigate to the login page, perhaps logging out first
            throw new IllegalStateException("This is not the login page");
        }
    }

    // The login page contains several HTML elements that will be represented as WebElements.
    // The locators for these elements should only be defined once.

    //    By usernameLocator = By.name("username");
    //    By passwordLocator = By.name("password");
     //   By loginButtonLocator = By.name("submit");

    // The login page allows the user to type their username into the username field
    public LoginPage typeUsername(String username) {
        // This is the only place that "knows" how to enter a username
        driver.findElement(By.name("username")).sendKeys(username);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to type their password into the password field
    public LoginPage typePassword(String password) {
        // This is the only place that "knows" how to enter a password
        //driver.findElement(passwordLocator).sendKeys(password);
        driver.findElement(By.name("password")).sendKeys(password);
        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to submit the login form
    public HomePage submitLogin() {
        // This is the only place that submits the login form and expects the destination to be the home page.
        // A seperate method should be created for the instance of clicking login whilst expecting a login failure. 
  //      driver.findElement(By.name("submit")).submit();

        // Return a new page object representing the destination. Should the login page ever
        // go somewhere else (for example, a legal disclaimer) then changing the method signature
        // for this method will mean that all tests that rely on this behaviour won't compile.
        return new HomePage(driver);    
    }

    // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
    public LoginPage submitLoginExpectingFailure() {
        // This is the only place that submits the login form and expects the destination to be the login page due to login failure.
    //    driver.findElement(By.name("submit")).submit();

        // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials 
        // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
        return new LoginPage(driver);   
    }

    // Conceptually, the login page offers the user the service of being able to "log into"
    // the application using a user name and password. 
    public HomePage loginAs(String username, String password) {
        // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("URL Goes Here");
        LoginPage login = new LoginPage(driver);
        HomePage a=login.loginAs("username","Password");


    }
} 

I reffered it from http://code.google.com/p/selenium/wiki/PageObjects 我从http://code.google.com/p/selenium/wiki/PageObjects引用了它

Try to add 尝试添加

try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

before HomePage a=login.loginAs("username","Password"); HomePage a=login.loginAs("username","Password");之前HomePage a=login.loginAs("username","Password"); .

It is not quite good approach, but it will help to find out where is the problem. 这不是一个很好的方法,但是它将有助于找出问题所在。 This is just a pause before next step. 这只是下一步之前的暂停。 You shouldn't use it in future, because it is not pretty good practice. 将来您不应该使用它,因为它不是很好的做法。 It is better to use wait for condition check this out for more information. 这是更好地使用wait for condition检查出更多的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Selenium Java传递用户名和密码以及用于登录的URL时出错 - Error in passing username and password along with the url for login using Selenium Java 在 Selenium 中使用带有用户名和密码的代理 - Use a proxy with username and password in Selenium 无法在selenium webdriver中找到元素(用户名和密码) - Unable to locate element (username and password) in selenium webdriver 检查用户名和密码时出错 - Error when checking for username and password 显示用于连接到服务器的用户名和密码的代码 - Code showing username and password for connecting to server 在Java代码中设置代理用户名和密码 - Set Proxy Username and Password in java code 使用数据提供者注释输入用户名和密码。在 selenium 中执行时出现 java.lang.ArrayIndexOutOfBoundsException 错误 - Using data provider annotation to input username and password.getting java.lang.ArrayIndexOutOfBoundsException error when executing in selenium Java中的用户名和密码不匹配错误消息 - Username and Password not Matching Error Messages in Java 这段代码如何检查存储在数据库中的密码和用户名是否相等? - How this code checks equality of the password and the username That stored in database? 如何在我的代码中修复密码/用户名身份验证? - How Do I fix the password/ username authentication in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM