MattHodson
MattHodson

Reputation: 786

Selenium: stale element reference: element is not attached to the page document

I'm using C# and Selenium Chromedriver to do a test project scraping Google Maps.

It works well until it gets to the second loop here:

var companyurl = company.GetAttribute("href");

Which returns back:

OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

Which I think is due to my list doing this, for some reason: enter image description here

This is all my code:

var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();

options.AddExcludedArgument("excludeSwitches");
options.AddExcludedArgument("useAutomationExtension");
options.AddArguments("ignore-certificate-errors");
options.AddArguments("start-maximized");
options.AddArguments("enable-automation");
options.AddArguments("--log-level=OFF");
//options.AddArguments("headless");
options.AddArguments("--disable-blink-features=AutomationControlled");

var driver = new ChromeDriver(chromeDriverService, options);
Actions builder = new Actions(driver);

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);

driver.Navigate().GoToUrl("https://www.google.co.uk/maps/search/lincoln+plumbers/");

try
{
    driver.FindElement(By.CssSelector("#yDmH0d > c-wiz > div > div > div > div.NIoIEf > div.G4njw > div.AIC7ge > form > div > div > button > span")).Click();
}
catch
{

}

IList<IWebElement> companies = driver.FindElements(By.XPath("//*[@id=\"pane\"]/div/div[1]/div/div/div[4]/div[1]/div/div/a"));

foreach (var company in companies)
{
    try
    {
        var companyurl = company.GetAttribute("href");
        driver.Navigate().GoToUrl(companyurl);
        var companyName = driver.FindElement(By.TagName("h1")).Text;
        Console.WriteLine(companyName);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}
Console.ReadLine();

Any help would be appreciated!

Upvotes: 0

Views: 190

Answers (1)

Conrad Albrecht
Conrad Albrecht

Reputation: 2066

As soon as you call this:

driver.Navigate().GoToUrl(companyurl);

... you make "stale" all element references you have collected, including companies. I'd guess you want to redesign your algorithm to extract and save all the info you need from the 1st page, before you make its element references stale by navigating to other pages.

Upvotes: 1

Related Questions