我正在寻找 Java 中的解决方案 - Selenium Webdriver..我创建了一个函数
public WebElement waitforElementCss(String locator)
{
WebDriverWait wait = new WebDriverWait(driver,10);
return wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(locator)));
}
我想做的是,让它像这样动态
public WebElement waitforElementCss(String type, String locator)
{
WebDriverWait wait = new WebDriverWait(driver,10);
return wait.until(ExpectedConditions.elementToBeClickable(By.type(locator)));
}
因此,我不是每次都调用 By.CssSelector、Xpath...我希望它从我调用的地方获取参数...我已经在 python 中做到了这一点,但由于某种原因在 Java 中我无法做到这一点..
最佳答案
您不需要为此使用字符串
:
public WebElement waitforElement(By locator)
{
WebDriverWait wait = new WebDriverWait(driver,10);
return wait.until(ExpectedConditions.elementToBeClickable(locator));
}
By
已经是定位机制的抽象。您不需要使用 string
使它们更加通用,使用上述方法,如下所示:
waitForElement(By.cssSelector("something"));
waitForElement(By.id("something"));
waitForElement(By.xpath("something"));
而不是:
waitForElement("xpath", "something");
waitForElement("id", "something");
waitForElement("css", "something");
更好,不是吗?例如,如果我拼错“xpath”,您不太可能会遇到手动错误。还使用内置框架类,因此您不会重复工作。
关于java - 有没有办法将变量与 By 连接起来。用于 Java 中的 Selenium Webdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26939821/