0

I am trying to use By in selenium as a String to get my element but it is giving me the error "'id(java.lang.String)' in 'org.openqa.selenium.By' cannot be applied to '(org.openqa.selenium.By)'"

Please assist me Here is my code:

package adta;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class LoginPage extends BasePage {

    public LoginPage(WebDriver driver) {
       super(driver);
    }

    public By getUserNameLocator(){
        return By.id("user-name");

    }


    public void open(){
        driver.get("https://www.saucedemo.com/");

    }
    public boolean isLoaded(){

        return wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(getUserNameLocator()))).isDisplayed();

    }


    public void login(String username, String password) {
        driver.findElement(By.id(getUserNameLocator())).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("login-button")).click();
    }
}

I have tried to search for answers online but no luck

1
  • In what line, as your source code has multiple calls to By.id, some of them use output of another method as argument? Commented Nov 29, 2022 at 18:55

1 Answer 1

1

Since your getUserNameLocator() method returns a By type object and ExpectedConditions.visibilityOfElementLocated() method receives a By type value, you can use that inside isLoaded() method as following:

public boolean isLoaded(){

    return wait.until(ExpectedConditions.visibilityOfElementLocated(getUserNameLocator())).isDisplayed();

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! That was exactly what I did and it worked. Thanks a million.!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.