English 中文(简体)
活动专用网站
原标题:Stale reference exception for EventFiringWebDriver listener

I am getting Stale reference exception(Element no longer valid), I am using c# - Webdriver for automation.

I am using Selenium for automation framework. In that I have used EventFiringWebDriver class of Selenium Event. that listens to every event of InterenetExplorerWebdriver (Like Click() or SendKeys()). In the events of the EventFiringWebDriver class like (ElementClicked,ElementValueChanged) I have implemented the logic that logs the WebElementEventArgs object s attributes to XML based log file. so at the end of the execution I can see the details of the execution of each test case in XSLT format and track functional errors.

code:-

public class SeleniumEventListener : EventFiringWebDriver
{
     public SeleniumEventListener(IWebDriver webDriver):base(webDriver)
     {
             ElementClicked += new EventHandler<WebElementEventArgs>SeleniumEventListener_ElementClicked);
//more events handled here..
 iii
   void SeleniumEventListener_ElementValueChanged(object sender,WebElementEventArgs e)
 {
           LogPassedStep(e)
   iii
   private LogPassedStep(e)
   {
          string title = e.Element.value;
            string status = "clicked"

         //XML based logging here.
   iii

iii

In my application which I automate using selenium. I have some text boxes and Buttons, The EventFiringWebDriver class XMLLogging works correctly for the textboxes through out the test case execution But for the Buttons Or Links some of the times it Gives the "Element no longer valid" Stale reference exception(The some buttons are of the type Expand collapse which causes DOM to change, and In my application I have a Task page which refreshes at regular interval in that the Stale reference exception comes often)

The Webdriver clicks works correctly throughout the execution but the EventFiringWebDriver fails to recognize the object and Stale reference exception comes

Please help me.. or please suggest me the way to get that details of the webpage objects (like button,link) so that I can implement the Logging functionality(I am using C#-webdriver)/

问题回答

StaleElementReferenceException means you are accessing attribute of the element, which no longer exists. You need to collect all needed attribute values for an element before the DOM changes.

因此,如果你想要:

  1. find element on page
  2. make element disappear (e.g. open another page)
  3. log that element s name

阁下:

  1. find element on page
  2. store element s name in the variable (my_elements_name)
  3. make element disappear (e.g. open another page)
  4. log that element s name, using my_elements_name variable, and not the element itself.

p:

from selenium.webdriver import Firefox


browser = Firefox()
browser.get( http://google.com )

element = browser.find_element_by_id( lst-ib )
element_name = element.get_attribute( name )

browser.get( http://en.wikipedia.org )

# this will raise  StaleElementReferenceException :
print element.get_attribute( name )

# and this will work:
print element_name
# prints out:  q 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签