0
public class LoginPage {

    WebDriver driver;

    @FindBy(xpath = "//input[contains(@class,'oxd-input') and @name='username']")
    private WebElement userName;

    @FindBy(xpath = "//input[contains(@class,'oxd-input') and @name='password']")
    private WebElement password;

    @FindBy(xpath = "//input[contains(@class,'oxd-button') and @type='submit']")
    private WebElement loginButton;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void enterUserName(String userNameValue) {
        userName.sendKeys(userNameValue);
    }

    public void enterPassword(String pwd) {
        password.sendKeys(pwd);
    }

    public void clickLoginBtn() {
        loginButton.click();
    }

}


@When("User logins in with correct credentials")
    public void user_logins_in_with_correct_credentials() {
        SeleniumManager.getInstance().getDriverPath("chromedriver");
        driver = new ChromeDriver();
        driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
        driver.manage().window().maximize();
        login = new LoginPage(driver);
        login.enterUserName("Admin");
        login.enterPassword("admin123");
        login.clickLoginBtn();

    }

enter image description here

Error Message:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@class,'oxd-input') and @name='username']"}

4 Answers 4

1

The exception thrown 'NoSuchElementException' can be due to pageload issue as there is nothing wrong with the webelement 'userName'. These are common issues faced in test automation services company while creating frameworks.

Try to use dynamic waits like below to avoid flaky failures in executions:

WebDriverWait wait = new WebDriverWait(driverChrome, 10);

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@class,'oxd-input') and @name='username']"))).click();

or

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[contains(@class,'oxd-input') and @name='username']")));

0

Solution :

  • You have used class names incorrectly
  • Why to try xpath? I suggest that should be the last option
  • Try with driver.findElement(By.name("Username")).sendKeys("ABC"); OR driver.findElement(By.cssSelector("input[name='Username']")).sendKeys("ABC");below as per your code
    @FindBy(name = "username")

OR


    @FindBy(xpath = "//input[(@class,'.oxd-input--active') and @name='username']")

15
  • Okay, Thanks. Is this whole string the class name ? oxd-input oxd-input--active or just part of it?
    – harry234
    Feb 3, 2023 at 11:46
  • THis locator still doesnt work :| No such element exception
    – harry234
    Feb 3, 2023 at 11:54
  • org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@class,'oxd-input--active') and @name='username']"}
    – harry234
    Feb 3, 2023 at 12:01
  • Not sure ,why its not working...everything looks fine.
    – harry234
    Feb 3, 2023 at 12:01
  • Can you try with placeholder instead of name property Feb 3, 2023 at 12:02
0

Try to use customize XPATH, I use the following code in python, hope it helps you:

driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")
driver.find_element(By.XPATH,"//input[@name='username']").send_keys("Admin")
driver.find_element(By.XPATH,"//input[@name='password']").send_keys("admin123")
driver.find_element(By.XPATH,"//button[@type='submit']").click()
0

None of the above code worked for me but my below code works for me as I was using only selenium to test without python. You need to add sleep timer for that after the website is open, 40seconds or 60seconds to enter the username and password and click login.

driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
//timer i kept as 60 you can keep 40
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                
//find username
                driver.findElement(By.xpath("//input[@placeholder='Username']")).sendKeys("Admin");
                
//find password
                driver.findElement(By.xpath("//input[@placeholder='Password']")).sendKeys("admin123");
                
//login button click
                
driver.findElement(By.xpath("//button[@type='submit']")).submit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.