Home > front end >  How pass locator as parameter
How pass locator as parameter

Time:09-16

I tried to create function which contain parameter with By type but i get error and i don't understand why

public void Set(By by, string cssselectorName)
{
    driver.FindElement(By.by(cssselectorName)).SendKeys("Hello");
}
  public void TestMethod1()
        {
            IWebDriver driver = new EdgeDriver();
            driver.Manage().Window.Maximize();
            Functions f = new Functions(driver);
            f.OpenBrowser();       
            f.Set(By.ClassName,"email_create");
        }

CodePudding user response:

Try changing it as following

public void Set(By locator, String textValue)
{
    driver.FindElement(locator).SendKeys(textValue);
}

UPD
I will bring some examples of passing By as parameter.
My code is in Java, but it is very close to C#.
I define addBtn By variable

private final By addBtn = By.cssSelector("div.modal-content div.add-button");

I pass it to method

public boolean clickAddButtonOnModalDialog() {
    return WebdriverUtils.clickVisible(driver, addBtn, 30);
}

The final method is:

public static boolean clickVisible(WebDriver driver, By locator, int timeout) {
    try {
        Wait<WebDriver> wait = new WebDriverWait(driver, timeout);
        wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).click();
        return true;
    } catch (Exception e) {
        ConsoleLogger.error("Failed to click on element"   e.getMessage());
        return false;
    }
}
  • Related