In my development of Selenium tests, I have come to dread the oh-so-common "Permission denied" errors. A quick scan of google search results indicates that there is likely some cross-domain issue, and a suggested workaround is using an experimental browser, like *iehta. I generally use *iehta anyway, and still seem to have problems with the "Permission denied" error.

In my most recent experience, testing against what seems to be an inconsistently slow and sometimes flaky web app, I have come to expect that "Permission denied" usually indicates that an element that I am trying to access is not yet accessible. For my app, which is heavily AJAXed, "waitforpagetoload" doesn't cut it. Since the AJAX scripts are still running, Selenium would wait until the end of time (or the timeout, whichever comes first) for the page to load and it would not.

So, I found a few people who solved this problem by creating a function that would wait for a specific element to load. I decided to go with it, and now my "WaitForElement" function is standard in my local Selenium Template for Visual Studio.

This function looks like this (this is C# code):

private void WaitForElement(string elementName)
for (int second = 0; ; second++)
if (second >= 120) Assert.Fail("timeout");

if (selenium.IsElementPresent(elementName)) break;
catch (Exception e) { }

I call this function the first time I am going to take action after the web app has has to re-load or re-render elements. I tend to pass it the element which I am wanting to interact with first.

So in my test, the steps tend to look like this:
selenium.Type("txtBox1", "username");

I have seen some other suggestions from people on stabilizing selenium tests, but would love to hear from anyone else who has worked out a way to make their selenium tests less fail-prone.