I have a Java program using Selenium and would like to use @FindBy()
, but I can't solve the problem. 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.
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
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
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.
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}
CodePudding user response:
I will add this code
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
thank you BMW83