Rip's Domain

Selenium and ElementNotVisibleException: One rule to keep yourself from going insane

Posted in Uncategorized by rip747 on April 5, 2014

I’m working on a project using Selenium and C#. Though really cool as hell, I have to say its nothing short of frustrating.

Take for instance the following error:

OpenQA.Selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with.

That little error took me an hour to figure out why it was happening. For some reason I would get this error when I was running my console app by double clicking on it, however i wouldn’t get the error when launching it through a schedule task.

The issue turned out to be something I completely overlooked and probably a golden rule when using Selenium:

“If you can’t see it, Selenium can’t click it”

So what doesn’t that mean exactly? In other words when I was running my application by launching it myself; firefox would open, go to the page and Selenium would attempt to click a button on the page. Turns out that even though the button was “physically” on the page, it was covered by some stupid static overlay hugging the bottom of the screen because of the window size. I only figured it out after I paused Selenium (using Thread.Sleep(5000);)  and manually scrolled the page down so I could physically see the button myself in the browser window.

Now it all made sense why everything was blowing up when I was running the application myself, but it would work fine through a scheduled tasks. I’m guessing through a schedule task, the browser is invoked headless so this whole “physically seeing” elements and browser window size doesn’t apply. Selenium must just see the page as whole since it doesn’t have the restriction of the browser window.

In the end, the way I got it all to work by forcing the page to scroll to the bottom using the IJavaScriptExecutor like so:

IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript(“window.scrollTo(0, document.body.scrollHeight);”);


By default, Selenium will automatically scroll the window to the element in order to make sure that it is visible in the browser window, however, because of this overlay, it actually needed to scroll about 100px more down the page in order for the element to actually be visible in the browser. Scrolling to the bottom was a hack, but it worked.