是否有一种方法(如果使用java(来刷新现有的元素,这已成为现实的元素。类似的东西,我有一个Angular应用程序可以删除并重新添加一个元素。有时,这会达到比赛状态,并给我一个陈旧的状态。我想避免睡觉以减慢事情的速度,但是,我可以每500毫秒或其他东西尝试一次。我看到发现了,但是当我尝试
之类的东西时driver.findElement(By.xpath(element.foundBy))
但这不起作用。如果我不确定最初使用了哪种XPath,该如何刷新?
如果页面刷新或包括您的WebElement的任何DOM的任何更改都会导致此类例外。硒不支持WebElement刷新(可悲的是(,因此您必须手工再次找到元素,在某些情况下可能是PITA。我使用
public WebElement refreshElement(WebElement element){
String sElement = element.toString().split("-> ")[1];
String locatorType = sElement.split(": ")[0];
if (locatorType.matches("css selector")) locatorType = "css";
String loc0 = sElement.split(": ")[1];
String theLocator = loc0.substring(0,loc0.length()-1);
System.out.println("Refreshing element with "+locatorType+": "+theLocator);
return locator.getElement(theLocator,locatorType);
}
并且似乎正常工作,因为webelement.tostring((会产生
之类的东西[[ChromeDriver: chrome on LINUX (****************************)] -> locatortype: locator]
getElement方法就像
public WebElement getElement(String locator, String type) {
if (type.equals("id")) {
return this.driver.findElement(By.id(locator));
}
else if (type.equals("name")) {
return this.driver.findElement(By.name(locator));
}
etc...
}
我可以通过提取定位器值并刷新元素来克服StaleelementReferenceException。
/*
* Example Element Info: [[ChromeDriver: chrome on MAC (7066c356a66e3d36c02336042e9ae3bd)] -> tag name: input]
* Get locator value from Element and find again to get over StaleElementReferenceException
*/
public WebElement refreshWebElement(WebDriver webDriver, WebElement webEl) {
String elementInfo = webEl.toString();
elementInfo = elementInfo.substring(elementInfo.indexOf("->"));
String elementLocator = elementInfo.substring(elementInfo.indexOf(": "));
elementLocator = elementLocator.substring(2, elementLocator.length() - 1);
System.out.println(elementInfo);
WebElement retWebEl = null;
if (elementInfo.contains("-> link text:")) {
retWebEl = webDriver.findElement(By.linkText(elementLocator));
} else if (elementInfo.contains("-> name:")) {
retWebEl = webDriver.findElement(By.name(elementLocator));
} else if (elementInfo.contains("-> id:")) {
retWebEl = webDriver.findElement(By.id(elementLocator));
} else if (elementInfo.contains("-> xpath:")) {
retWebEl = webDriver.findElement(By.xpath(elementLocator));
} else if (elementInfo.contains("-> class name:")) {
retWebEl = webDriver.findElement(By.className(elementLocator));
} else if (elementInfo.contains("-> css selector:")) {
retWebEl = webDriver.findElement(By.cssSelector(elementLocator));
} else if (elementInfo.contains("-> partial link text:")) {
retWebEl = webDriver.findElement(By.partialLinkText(elementLocator));
} else if (elementInfo.contains("-> tag name:")) {
retWebEl = webDriver.findElement(By.tagName(elementLocator));
} else {
System.out.println("No valid locator found. Couldn't refresh element");
}
return retWebEl;
}
尝试以下类似:
WebElement yourElement = driver.findElement(By.xpath("xpath here"));
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(500, TimeUnit.MILLISECONDS);
wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(yourElement )));