I finally found the reason for the exception that's been bugging me for a while. I had accidentally called the test twice - and doing it again caused problems (in my environment). I now succeeded in building a repro (albeit using 2 URLs). Wonder if I am missing any Selenium-mysteries or if I should post it as a bug to the recommended places.
The following C# needs webdriver4 alpha (WebDriver3 has the same problem, but can't read logs, so I can't make the point as clear there)
I have a simple ol or ul and try to move to the first link in the list:
using System;
using System.Diagnostics;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using static OpenQA.Selenium.By;
using static OpenQA.Selenium.LogType;
using OpenQA.Selenium.Interactions;
using System.Linq;
namespace Stale
{
class Program
{
static void Main()
{
ChromeOptions options = new ChromeOptions();
options.SetLoggingPreference("driver", LogLevel.All);
options.AddArgument("--allow-file-access-from-files");
ChromeDriverService BSVC = ChromeDriverService.CreateDefaultService("./", "chromedriver.exe");
ChromeDriver driver = new ChromeDriver(BSVC, options);
var ACTIONS = new OpenQA.Selenium.Interactions.Actions(driver);
driver.Navigate().GoToUrl("http://mbaas.de/stale/ol.html");
CheckLogs(driver); /* call once to get rid of irrelevant entries */
IWebElement link1 = driver.FindElementByCssSelector("#links > li > a");
ACTIONS.MoveToElement(link1).Perform();
driver.Navigate().GoToUrl("http://mbaas.de/stale/ul.html");
System.Threading.Thread.Sleep(5000); /* just to make the point that giving it time to process changes does not help */
IWebElement link2 = driver.FindElementByCssSelector("#links > li > a");
try
{
ACTIONS.MoveToElement(link2).Perform();
}
catch (OpenQA.Selenium.StaleElementReferenceException)
{
Console.WriteLine("Exception! See log for details...");
}
CheckLogs(driver);
// driver.Dispose(); // Commented this line so that we keep seeing the conle (with the msg about logging the exception)
}
private static void CheckLogs(ChromeDriver Driver)
{
File.Delete("./driver.log");
System.Collections.Generic.List<LogEntry> logs = Driver.Manage().Logs.GetLog(LogType.Driver).ToList();
using StreamWriter sw = File.CreateText("./driver.log");
foreach (var entry in logs)
{
sw.WriteLine(entry.Message);
}
}
}
}
I am quite a n00b with C# and also with these driver logs, so some of may conclusions may be wrong - but if I try to apply the scheme seen in the first Find/Move-operation (lines 135 and 143) to the second run (525/533), it looks like the internal ids from the first run are re-used in the 2nd MoveTo. But clearly I'm passing a different value from my code.
Did I so something wrong or could it be a "real" bug?
(BTW, it's not related to ChromeDriver, I can repro with Firefox or Edge as well)