En mi aplicación C# para utilizar selenium web driver obtengo este error:
OpenQA.Selenium.StaleElementReferenceException: stale element
referencia: el elemento no está unido al documento de la página
en este código:
IWebElement e = driver.FindElement(By.XPath(link_click), 10);
e.Click();
la línea de error está en el e.Click()
pero este es un procedimiento que se ejecutó con éxito en el mismo enlace especificado por XPath antes, pero falló en el último intento! así que lo que este error significa y cómo solucionarlo?
Significa que o bien el elemento ha cambiado en la página, o el elemento se ha eliminado, referencia completa en este enlace http://www.seleniumhq.org/exceptions/stale_element_reference.jsp
Una forma de hacer frente a esto, usted podría poner reintento, probablemente algo como
bool staleElement = true;
while(staleElement){
try{
driver.FindElement(By.XPath(link_click), 10).Click();
staleElement = false;
} catch(StaleElementReferenceException e){
staleElement = true;
}
}
I'd el mismo problema cuando yo estaba haciendo selector de fecha, con uno de los sitio. I'd para obtener todos los activos (o habilitado) botones del selector de fecha y haga clic en cada uno de eso. Cuando me iteró los elementos que estaba recibiendo rancio. I'd re reiterar manteniendo otra lista. Esto puede haber ocurrido porque una vez que el List
@Test
public void datePickerTest() throws InterruptedException{
driver.get(baseURL);
// click on flights tab
genericMethod.getElement("tab-flight-tab-hp", "id").click();
// click departing date, such that the date picker is loaded in the dom
genericMethod.getElement("flight-departing-hp-flight", "id").click();
Thread.sleep(700);
// pass the collected xpath where you can find all the dates which are enabled
String xpath=".//*[@id='flight-departing-wrapper-hp-flight']/div/div/div[2]/table/tbody/tr/td/button[not(@disabled)]";
List<WebElement> activeDatesWebElement = genericMethod.getElements("xpath", xpath);
System.out.println("Number of Active Dates " + activeDatesWebElement.size());
// work around for an element when it is found stale
List<String> activeDateListAsString = new ArrayList<String>();
for(WebElement temp : activeDatesWebElement){
activeDateListAsString.add(temp.getText());
}
// iterate all element in the list received, which is kept in list
for(String temp : activeDateListAsString){
genericMethod.getElement("flight-departing-hp-flight", "id").click();
Thread.sleep(500);
String selectDateXpath=".//*[@id='flight-departing-wrapper-hp-flight']"
+ "/div/div/div[2]/table/tbody/tr/td/button[text()='"
+temp+"']";
genericMethod.getElement(selectDateXpath, "xpath").click();
Thread.sleep(500);
}
}
Obtendrá este error si el elemento no está presente mientras se carga la página. Debe esperar hasta que el elemento esté listo:
public static Func<IWebDriver, IWebElement> Condition(By locator)
{
return (driver) => {
var element = driver.FindElements(locator).FirstOrDefault();
return element != null && element.Displayed && element.Enabled ? element : null;
};
}
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
var elementU = wait.Until(Condition(By.Name("j_username")));
elementU.Click();