简体   繁体   中英

C# Selenium element is not attached

I want to change the language from a website. Below you can see the html source code, the c# selenium code and the error. The error stops my programm, but the strange thing is that the language changed. I hope you can help me!

html source code

<select class="_fsoey">
   <option value="de">Deutsch</option>
   <option value="en">English</option>
   <option value="it">Italiano</option>
   <option value="nb">Norsk</option>
   <option value="nl">Nederlands</option>
   <option value="pl">Polski</option>
</select>

C# Selenium Code

driver.FindElement(By.XPath("//select[@class='_fsoey']")).Click();
driver.FindElement(By.XPath("//option[contains(.,'English')]")).Click();

ERROR

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

The element with which you are trying to interact is within a <select> tag. So to select an option from the Dropdown you need to create a select object and invoke either of the methods SelectByValue() or SelectByText() as follows :

using OpenQA.Selenium.Support.UI;

// Identify the drop downdown element
var cities = driver.FindElement(By.XPath("//select[@class='_fsoey']"));
//create select element object 
var selectElement = new SelectElement(cities);

//select by value
selectElement.SelectByValue("en"); 
// select by text
selectElement.SelectByText("English");

If you would have sent the stack trace, then I would be able to give a more definite answer, but still, it looks pretty unlikely that the exception is thrown from one of these 2 lines, and I'll explain why shortly. My guess is that the exception is thrown by a later operation.

First, you say that the language does gets selected, which means that the last click was actually performed. But that's not the only reason.

Each of these 2 lines perform 2 calls to WebDriver: a call to IWebDriver.FindElement, which returns IWebElement, followed by a call to IWebElement.Click on the object that was returned from the first call. Note that StaleElementReferenceException can only be thrown from methods on IWebElement, which means that IWebDriver.FindElement succeeded. Now, StaleElementReferenceException means that the element which was originally found was later removed from the page. Because you don't perform any operation between the 2 calls, this is very unlikely.

The only case in which this can happen is if the page performed some kind of an asyc operation that removed the element between these 2 method calls of each line. But then again, in this case it wouldn't make sense that the language was selected correctly.

For the small chance that I'm still wrong, I'll give you a tip that will help you investigate the root cause further. If you catch the exception and examine the IWebDriver.PageSource property, you can see the state of the DOM and see whether the element was actually removed or not (at least in Chrome). if you examine it also before the call to FindElement you'll be able to compare them and see if something has changed. If you save the value of the PageSource property to a .html file, then you can open the file in the browser and examine the DOM in the Dev Tools. This should help you understand why you're getting this exception.

You can use Select method,

Select dropdown = new Select(driver.findElement(By.xpath("//select[@class='_fsoey']")));
dropdown.selectByValue("en");  

Where you can select value of drop down by two method : SelectByValue and SelectByIndex

The problem might be that between the time you do a find element cities to the time where you select from the dropdown the page is getting changed or refreshed which is why you are getting the stale element exception. Try finding the element just before you select from the dropdown. If that is not the case then please provide some more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM