Reputation: 1299
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 ?
Here is another try.
Upvotes: 0
Views: 1195
Reputation: 116
Try implicit wait
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
Upvotes: 0
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
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.
Upvotes: 1
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
Reputation: 37
I think the best way is to use XPath. You can create a delay until XPath element is found.
Upvotes: 0
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