Thanks for the pointer to using findelements. I think you will also need a wait logic if you do not want it to return too soon.
here is my solution in c #.
Basically, it restores the wait logic in the selenium library - unfortunately, the method that makes the wait is not displayed (it is annoying that it also incorrectly cancels exceptions and loses stack traces!).
You probably have to change it a bit for java - I donβt know what the selenium API is when you cannot just execute functions.
Waiting for re-implementation:
private IWebElement WaitAndSeeIf(Func<IWebElement> canGet) { var end = DateTime.Now.AddSeconds(1); IWebElement element; while (true) { element = canGet(); if (element != null) break; var time = DateTime.Now; if (time > end) break; Thread.Sleep(200); } return element; }
call code:
var dashboardButton = WaitAndSeeIf(() => { var elements = Driver.FindElements(By.XPath("//button[contains(.//*,'Dashboard')]")); return elements.Any() ? elements.Single() : null; });
So far, I have found this useful in several places (checking for dialogs in extjs, etc.), but still they had to quench and configure it. I believe that the best part to do is force it to accept an implicit wait time, as indicated in another answer.
source share