gpt4 book ai didi

java - Selenium 用户名密码错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:11 25 4
gpt4 key购买 nike

我正在编写一段代码,将用户名和密码输入 URL 并提交该页面。但我不断收到此错误

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

下面是代码

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");


}
}

我从 http://code.google.com/p/selenium/wiki/PageObjects 引用它

最佳答案

尝试添加

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

首页之前 a=login.loginAs("用户名","密码");

这不是很好的方法,但有助于找出问题所在。这只是下一步之前的暂停。您将来不应该使用它,因为这不是很好的做法。最好使用等待条件检查this获取更多信息。

关于java - Selenium 用户名密码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com