我正在尝试编写一个通用Web驱动程序等待等待元素可点击。但是我发现了网络驱动程序等待写入特定于By.id或By.name的等待。如何在Selenium中编写通用Web驱动程序等待
假设下面是两个WebElements
public WebElement accountNew() {
WebElement accountNew = driver.findElement(By.xpath("//input[@title='New']"));
waitForElementtobeClickable(accountNew);
return accountNew;
}
public WebElement accountName() {
WebElement accountName = driver.findElement(By.id("acc2"));
waitForElementtobeClickable(accountName);
return accountName;
}
下面是广义waitofrelementtobeclickable。
public static void waitForElementtobeClickable(WebElement element) {
try {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(element));
System.out.println("Got the element to be clickable within 10 seconds" + element);
} catch (Exception e) {
WebDriverWait wait1 = new WebDriverWait(driver, 20);
wait1.until(ExpectedConditions.elementToBeClickable(element));
System.out.println("Got the element to be clickable within 20 seconds" + element);
e.printStackTrace();
}
}
但它似乎没有工作。任何关于如何为xpath,或id,或class或Css写一个通用代码的建议都可以写出来?
做什么你的意思是“似乎不起作用”?你有错误吗? – Guy
不,没有错误,但对waitforelementclickable的调用只是绕过,并没有通过实际等待10或20秒的过程。示例:登录到Salesforce应用程序后,我希望顶部面板中的userName是可点击的,以便我可以点击它,然后单击注销。但是登录后页面仍然正在加载,程序只是终止,说找不到元素。但是如果我给出20秒的明确睡眠,它就会起作用。所以xpath/locator不是问题。 – Ronnie