[英]Why doesn't Selenium @FindBy work with name, xpath, css
I have a Java program using Selenium and would like to use @FindBy()
, but I can't solve the problem.我有一个使用 Selenium 的 Java 程序,想使用@FindBy()
,但我无法解决问题。 My program is composed of several lines.我的程序由几行组成。 I would like to share tasks between classes.我想在班级之间分享任务。 And for that, I started with identity verification.为此,我从身份验证开始。 I created this class which includes login and password.我创建了这个 class,其中包括登录名和密码。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class LoginPageNew {
WebDriver webDriver;
public LoginPageNew(WebDriver driver) {
this.webDriver = driver;
}
@FindBy(css = "[name='username']")
@CacheLookup
WebElement username;
@FindBy(how = How.NAME, using = "password")
@CacheLookup
WebElement password;
@FindBy(how = How.XPATH, using = "//button[normalize-space()='Login']")
@CacheLookup
WebElement submit_btn;
public void loginPage() {
username.sendKeys("Admin");
password.sendKeys("admin123");
submit_btn.click();
}
}
And I created this class我创建了这个 class
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserFactory {
static WebDriver driver;
public static WebDriver startBrowser(String browserName, String url) {
if (browserName.equals("chrome")) {
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(url);
return driver;
}
}
And finally this test class最后这个测试 class
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class VerifyLoginPage {
@Test
public void checkValidUser() {
WebDriver webDriver = BrowserFactory.startBrowser("chrome", "http://opensource-demo.orangehrmlive.com/");
LoginPageNew pageNew = PageFactory.initElements(webDriver, LoginPageNew.class);
pageNew.loginPage();
}
}
For me, I would like to work with the site "https://opensource-demo.orangehrmlive.com/" with login and password "Admin" and "admin123" with this architecture to do the test.对我来说,我想使用具有此架构的登录名和密码“Admin”和“admin123”的站点“https://opensource-demo.orangehrmlive.com/”来进行测试。
And I used我用过
@FindBy(name="username"), @FindBy(css = "[name='username']"),
@Find By(xpath = "//input[@placeholder='Username']")
@Find By(xpath = "(//input[@placeholder='Username'])[1]")
@Find By(how = How. NAME, using = "username")...
But there is no solution, always the same error.但是没有解决办法,总是一样的错误。
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[name='username']"}
(Session info: chrome=109.0.5414.76)
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
Build info: version: '4.7.2', revision: '4d4020c3b7'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.16'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [f3cd14b65540143dd77567fdbdd70945, findElement {using=css selector, value=
[name='username']}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.76,
chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir:
C:\Users\hp\AppData\Local\T...}, goog:chromeOptions: {debuggerAddress: localhost:64620},
networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy:
Proxy(), se:cdp: ws://localhost:64620/devtoo..., se:cdpVersion: 109.0.5414.76, setWindowRect:
true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script:
30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true,
webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
I will add this code我将添加此代码
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
thank you BMW83谢谢宝马83
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.