I have a number of tests which are run using Selenium and WinAppDriver. Sometimes the test gets passed, often Selenium doesn't find an element or click the wrong element even though it had passed the same test before.
I'm not sure why this happening. It could be selenium but it is more than likely my code.
Any suggestions on how I can make my tests tighter to prevent this in the future?
I have a BaseClass where I've SetUp()
Method and The base class inherited in UnitTestClass.
In BaseClass, I check if I can click a "Maximize" element if it can't then I catch it and ignore it. Sometimes it works fine and sometimes it clicks on the top left of my screen
following is my code:
BaseClass
public class ExcelBaseClass
{
private string excelId = OfficeVersion.Excel();
private const string AppDriverUrl = "http://127.0.0.1:4723";
public static WindowsDriver<WindowsElement> excelSession;
public static WebDriverWait webDriverWait;
public static DesiredCapabilities appCapabilities = new DesiredCapabilities();
[TestInitialize]
protected virtual void SetUp()
{
try
{
appCapabilities.SetCapability("app", excelId);
var initialSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);
var capabilities = new DesiredCapabilities();
capabilities.SetCapability("app", "Root");
excelSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), capabilities);
CommonMethods.keyCheck(excelSession);
webDriverWait = new WebDriverWait(excelSession, TimeSpan.FromSeconds(60));
CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Create error when launching Excel");
try
{
webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElementByName("Maximize"))).Click();
}
catch (Exception)
{
//ignore
}
}
catch (Exception)
{
CommonMethods.ExceptionHandler("WinApp Driver failed to load Excel", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
}
}
UnitTestClass
[TestMethod]
public void newExcelWorkbook()
{
try
{
base.SetUp();
webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElementByName("Blank workbook"))).Click();
webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElementByName("Create"))).Click();
webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElementByName("New"))).Click();
webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElementByName("E Sample Data"))).Click();
CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Error appeard while selecting the E Sample Data button");
}
catch (Exception)
{
CommonMethods.ExceptionHandler("Couldn't find element", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
TearDown();
}
2 Answers 2
This sounds like two different issues. 1. Sometimes you get a NoSuchElementException
. 2. Sometimes selenium clicks the wrong element.
I'll focus on the 1st issue as the 2nd issue would probably need more debugging information.
You have a problem with this line:
webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElementByName("Maximize"))).Click();
First, excelSession.FindElementByName(...)
will be called, the result of that will be passed to the ElementToBeClickable
method, and that result is passed to Until
which will run that action until it returns true. This is a problem because excelSession.FindElementByName
throws an exception if the element doesn't exist, so the result can't ever be passed to ElementToBeClickable
.
The easiest solution is to pass a By
to this method, as that version of ElementToBeClickable
will wait until the By
successfully finds the element. Or to be more accurate, it will return false if the element doesn't exist.
By maximizeBy = By.Name("Maximize");
webDriverWait.Until(
ExpectedConditions.ElementToBeClickable(excelSession.FindElement(maximizeBy)).Click();
That should take care of your issue with it sometimes not clicking the maximize button due to a NoSuchElementException
.
Another thing that is likely causing you a lot of grief, is you're eating way too many Exceptions
, and then hiding the real exception by trying to guess why an exception was being thrown, and logging that. Generally speaking, try to avoid using Exception
s as logical paths. If an exception gets thrown, feel free to catch a specific Exception (something like NoSuchElementException
, not Exception
), do some logging or any cleanup that needs to happen, but still throw or attach the original exception message to your exception. Otherwise your going to run into confusing debugging problems.
Best of luck!
This is a pretty old question, but I just ran into this issue where Selenium was clicking on the wrong location on the page. It wasn't just Selenium though, pure JQuery did the same thing.
I was trying to test my site using the resolution and sizing from an iPhone. options.EnableMobileEmulation("iPhone 6/7/8");
I could manually click the element just fine. But stepping through my code, even though the right element was being found, when the click event was called a different location on the page was clicked.
I finally was able to work around my issue by increasing the size of the element to be clicked. It was originally an a tag < a href="/logout">Logout< /a>.
I changed it to < a href="/logout">Logout from this website< /a>. And that solved the problem, with the extra text expanding the element selenium was able to click it correctly.
My assuming is that the sizing code must introduce some sort of element location bug in either selenium or Chrome which is most noticeable on small elements.
Explore related questions
See similar questions with these tags.
ExpectedConditions.ElementExists(By selector)
before you wait for it to be clickable. You should be more specific about theExceptions
you're catching so you don't eat everything. My guess in this case you're getting aNoSuchElementException
becauseElementToBeClickable
expects the element to already be there. So you have to check for that first. I hope that made sense, give it a try and reply how it goes.By maximizeBy = By.Name("Maximize"); webDriverWait.Until(ExpectedConditions.ElementExists(maximizeBy)); webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElement(maximizeBy)).Click();
The documentation makes it sound like you might be able to pass theBy
toElementToBeClickable
to avoid the exception also