Common Selenium C# Exceptions and Fixes

Exception 1:

The process cannot access the file ‘bin\Debug\chromedriver.exe’ because it is being used by another process.

 

The Problem 

The problem here is that your ChromeDriver is being used by another process. So when it’s trying to be copied, it can’t because it’s being used.

 

The Solution 

Option A

1.    Open Task Manager and kill all chromedriver.exe processes 

Option B

1.    Open Powershell command line.

2.    Execute command: Get-Process chromedriver | Stop-Process

3.    This will kill all processes at once 

Exception 2:

System.InvalidOperationException : unknown error: Chrome version must be…

 

Error Message

System.InvalidOperationException : unknown error: Chrome version must be >= 54.0.2840.0

 

The Problem

Another issue with compatibility of ChromeDriver with Chrome Browser. In this case it was Chrome version 48.0 not working with ChromeDriver version 2.27

 

The Solution

Upgrade your Chrome browser to the version supported by ChromeDriver. For example, ChromeDriver 2.27 supports Chrome 54 – 56 according to their release notes.

 

Exception 3: ExpectedConditions’ is obsolete: ‘ implementation… deprecated

 

Error message

ExpectedConditions’ is obsolete: ‘The ExpectedConditions implementation in the .NET bindings is deprecated and will be removed in a future release. This portion of the code has been migrated to the DotNetSeleniumExtras repository on GitHub (https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras)’

 

The problem

Winter is coming! And so is Selenium 4.0 🙂 That’s exciting news because it means that it’s getting better. However, this also means that there are some things that will be deprecated. ExpectedConditions.cs will be moving to a different Nuget package. Right now, if you are using Selenium 3.X, this is just a warning. Later, you will not be able to do this.

 

The Solution

You can resolve the issue by installing nuget “DotNetSeleniumExtras”  and import in all the classes where you have used ExpectedConditions.

 

 

Exception 4: NoSuchElementException: no such element: Unable to locate element

 

Error message

OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:”partial link text”,”selector”:”Master Automation With Selenium”}
(Session info: chrome=81.0.4044.138)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.18362 x86_64)

 

The problem

The element cannot be found on the page. This can happen when you are using an incorrect locator for your element, or when the element has not been yet loaded on the page.

 

The Solution

This problem has two possible solutions:

 

Solution A

If the problem is that the locator is incorrect, try using a different locator (by Id, Name, Xpath etc) and make sure that the locator corresponds to a single element on the page.

 

Solution B

If the problem is related to the loading time, add a wait, so Selenium will wait a few seconds before throwing the exception if the element is not found.

·         Implicit wait (not really recommended)

WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(5));

Or

Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

·         Explicit wait

First, download the DotNetSeleniumExtras.WaitHelpers Nuget package.
Add this using statement to your class:

using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

Then add the explicit wait to your method:

WebDriverWait _wait =  new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
_wait.Until(ExpectedConditions.ElementIsVisible(locator));

 

Exception 5: Could not copy “C… chromedriver.exe” to {some path}..

 

Error message

Could not copy file “C:\…chromedriver.exe” to “…\chromedriver.exe

 

Solution

See this error above “Unable to copy file … chromedriver.exe”. It’s the same exact problem

 

 

Exception 6: System.TypeInitializationException

 

System.TypeInitializationException: System.TypeInitializationException: The type initializer for ‘Framework.Browser’ threw an exception. —> OpenQA.Selenium.WebDriverException: Failed to start up socket within 45000 ms. Attempted to connect to the following addresses: 127.0.0.1:7055.

Or something like this?

OpenQA.Selenium.WebDriverException : Unexpected error. System.Net.WebException: Unable to connect to the remote server —> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it

Today is your lucky day because I am going to help you resolve these really annoying errors!

 

The Problem

The problem is a standard compatibility issue between Selenium WebDriver and your version of a browser. As I am writing about this, the latest WebDriver version is 2.53.1. The latest Chrome version is 51.0 and the latest Firefox version is 46.0. Therefore, the Selenium WebDriver API does not get updated as often as Chrome or Firefox.

When different types of browsers such as Chrome or Firefox get updated, there is a chance that this update will break the Selenium API.

In our industry, this is known as a regression. Therefore, when you get the latest version of your browser, you may be using a Selenium WebDriver version that does not support the latest features of that browser. At this point, you will receive an ugly error that looks like this:

System.TypeInitializationException

Now, you will spend the next day wondering how your Selenium code used to work yesterday and today it just stopped working.

 

The Solution

Downgrade your appropriate browser to the version supported by the latest Selenium Driver. This might be Firefox, ChromeDriver, IEDriver and so on.

 

Exception 7: An exception with a null response was thrown sending an HTTP request to the remote…

 

Selenium.WebDriverException: ‘A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:25675/session. The status of the exception was ReceiveFailure, and the message was: The underlying connection was closed: An unexpected error occurred on a receive.’

 

InnerException

Unable to connect to the remote server

 

The Problem

You may have noticed that this page has one trend. Compatibility of Selenium WebDriver with a browser. This the largest burden in many of my behind, and has been for years now. This will probably continue until Selenium WebDriver is a W3C standard that is integrated into each browser. Until then, we will keep having these issues.

This error occurred to me with:

·         Chrome 56

·         ChromeDriver 2.27

·         Selenium WebDriver 3.0.1.0

 

The Solution

·         Update your Chrome browser to version 57.0

·         This did the trick for me. Keep in mind that maybe your versions of browser and ChromeDriver may be different. However, if you receive this error, just upgrade your browser until it fits into the support matrix of ChromeDriver release logs.

 

Exception 8: Element is not clickable at point (X, Y). Other element would receive the click…

 

Error Message

System.InvalidOperationException : unknown error: Element is not clickable at point (111, 700). Other element would receive the click: <div class=”et_social_heading”>…</div>  (Session info: chrome=45.0.2454.93)  (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64)

 

The Problem

The problem here is that the element that you are trying to click is either covered by another element, or simply not visible. Here is an example. If you go to ultimateqa.wpmudev.host and try to click the Start Here button when you are a bit scrolled down, the header covers up the button. As a result, even though the button is on the page, it is not clickable because the header is covering it up.

The Solution

 

To fix this problem, you need to make the element visible. Specifically,  Selenium tries to click on the exact center of the element. Your options are to scroll up, or hover over an element to close it, or minimize an expanded element. Simply put, make sure that your element is visible for clicking. The center of the element that you want to interact with should not be obstructed by another element.

 

Or you can use the Action class to perform the click operation on the element.

 

 

Common Selenium Exceptions Cheat Sheet

 

  • StaleElementReferenceException: Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.
  • InvalidElementStateException: This Selenium exception occurs if a command cannot be finished as the element is invalid.
  • InvalidSessionIdException: Takes place when the given session ID is not included in the list of active sessions, which means the session does not exist or is inactive either.
  • InvalidSwitchToTargetException: Happens if frame or window target to be switched does not exist.
  • JavascriptException: This problem happens when executing JavaScript supplied by the user.
  • JsonException: Happens when you afford to get the session capabilities where the session is not created.
  • MoveTargetOutOfBoundsException: Takes place if the target provided to the ActionChains move() methodology is not valid. For example: out of document.
  • NoAlertPresentException: Happens when you switch to no presented alert.
  • NoSuchAttributeException: Occurs when the attribute of element could not be found.
  • NoSuchContextException: Happens in mobile device testing and is thrown by ContextAware.
  • NoSuchCookieException: This exception is thrown if there is no cookie matching with the given path name found amongst the associated cookies of the current browsing context’s active document.
  • NoSuchElementException: Happens if an element could not be found.
  • NoSuchFrameException: Takes place if frame target to be switch does not exist.
  • NoSuchWindowException: Occurs if window target to be switch does not exist.
  • NotFoundException: This exception is subclass of WebDriverException. It happens when an element on the DOM does not exist.
  • RemoteDriverServerException: This Selenium exception is thrown when server do not respond due to the problem that the capabilities described are not proper.
  • ScreenshotException: It is impossible to capture a screen.
  • ScriptTimeoutException: Thrown when executeAsyncScript takes more time than the given time limit to return the value.

 

 

 


Comments

Popular posts from this blog

Introduction to Visual Testing

Mobile App Testing & Tools