根据下面的代码,我需要定期使用等待元素。有什么方法可以通过调用方法来重新使用此代码吗?
如有任何帮助或建议,我们将不胜感激。
public static By header = By.xpath("//*[@id=\'content\']/h1");
public static void ClickLink_Accounts() throws IOException {
WebDriverWait wait = new WebDriverWait(driver, 50);
WebElement wait2 = wait.until(ExpectedConditions.elementToBeClickable(header));
find(Accounts).isDisplayed();
CaptureScreenshot.Screenshot(driver,"Application HomePage-");
}
public static void ClickLink_Tasks() throws IOException {
WebDriverWait wait = new WebDriverWait(driver, 50);
WebElement wait2 = wait.until(ExpectedConditions.elementToBeClickable(header));
find(Tasks).isDisplayed();
CaptureScreenshot.Screenshot(driver,"Application HomePage-");
}
最佳答案
我认为你正在寻找的是这样的东西
public static void ClickLink_Accounts() throws IOException
{
waitForHeader();
find(Accounts).isDisplayed();
CaptureScreenshot.Screenshot(driver, "Application HomePage-");
}
public static void ClickLink_Tasks() throws IOException
{
waitForHeader();
find(Tasks).isDisplayed();
CaptureScreenshot.Screenshot(driver, "Application HomePage-");
}
public static void waitForHeader()
{
new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(header));
}
但是...当我查看您的两个 ClickLink* 方法时,我看到很多重复的代码。我会寻找一种方法将这两种(也许还有其他 future 的方法)组合成一个接受参数的方法。我假设 Accounts
和 Tasks
是 By
定位器,因为您要传递到方法 find()
中?如果是这样,您可以这样做
public static void ClickLink(By locator) throws IOException
{
waitForHeader();
find(locator).isDisplayed();
CaptureScreenshot.Screenshot(driver, "Application HomePage-");
}
并显着简化您的代码。请参阅DRY了解更多信息。
关于java - 我如何在方法中使用 (WebElement webdriverWait Until element is clickable) 并调用该方法以供重新使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53671919/