Adam
Adam

Reputation: 1299

OpenQA.Selenium.WebDriverException: 'Cannot click on element'

I have this line of code.

driver.FindElement(By.Id("BCA-button")).Click();

This was working fine at 'home'.

I am using these libraries in C# Unit Test project.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;

The same code stopped working in 'office' and gives this error.

OpenQA.Selenium.WebDriverException: 'Cannot click on element'

The only difference between my 'home' and 'office' environments are, I have bigger monitors in office and high speed internet.

Not sure, why these factors should affect this line of code. Same code was working yesterday in 'home' and it throws error in 'office' today.

Any thoughts ?

Added Snapshot for more clarity.

Here is another try.

I am using 'InvisibilityOfElementLocated'

Upvotes: 0

Views: 1195

Answers (6)

Salman Arshad
Salman Arshad

Reputation: 116

Try implicit wait

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);

Upvotes: 0

Agent Shoulder
Agent Shoulder

Reputation: 585

The reason you are getting the exception:

OpenQA.Selenium.WebDriverTimeOutException: 'Cannot click on element'

Is that the WebDriver is waiting for the element, but cannot locate it before reaching the timeout limit.

Is there any other element wrapping or covering the element you are trying to click?

Upvotes: 0

Adam
Adam

Reputation: 1299

I found the problem. If the screen resolution display settings, text size is not 100% (recommended settings) then "Selenium Web Driver" fails to perform the click event.

Display Setting on Screen Resolution

Upvotes: 1

Pratik
Pratik

Reputation: 357

Different ways of Clicking on Element is explained nicely here : Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click

I always use the 5th way when element.click() doesn't work even when the element is available and explicit wait is applied to element.

Upvotes: 0

lucas rshane
lucas rshane

Reputation: 37

I think the best way is to use XPath. You can create a delay until XPath element is found.

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Induce WebDriverWait and ElementToBeClickable()

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement elebutton = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("BCA-button")));
elebutton.Click();

You need to import this library.

using SeleniumExtras.WaitHelpers;

Upvotes: 0

Related Questions