microsoft / WinAppDriver

Windows Application Driver
MIT License
3.65k stars 1.4k forks source link

WebDriverWait doesn't wait for my element to appear #1603

Open cwk2 opened 2 years ago

cwk2 commented 2 years ago

When I attempt to use WebDriverWait to wait for an element to be displayed before interacting with it, I get an error immediately, and the wait doesn't occur.

var waitForMe = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
waitForMe.Until(x => x.FindElement(By.Name("Profile")).Displayed);

OpenQA.Selenium.WebDriverException: 'An element could not be located on the page using the given search parameters.'

Am I doing something wrong or is this a known issue? Thanks!

cwk2 commented 2 years ago

If I let the screen load - I can find the element using

var element = driver.FindElementByName("Profile");

So its not an issue of mis-identification of the element, it's a problem with the wait..

Shakevg commented 2 years ago

@cwk2 Try next code:

var waitForMe = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
waitForMe.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Name("Profile")));
cwk2 commented 2 years ago

@Shakevg - that worked, thank you! Do you have any explanation as to why my initial code didn't work? Would be helpful for my understanding!

Shakevg commented 2 years ago

@cwk2 I suppose answer here (Issue that you get Displayed from a null element that throws WebDriverException that not handle by WebDriverWait): https://stackoverflow.com/a/21004586/8951897

Source codes of WebDriverWait and VisibilityOfAllElementsLocatedBy: https://github.com/SeleniumHQ/selenium/blob/702b3aae7385adf331d75cdeacd79720cf6b9c84/java/src/org/openqa/selenium/support/ui/WebDriverWait.java#L126 https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras/blob/dc402a5d7b9d7e78a9ce293bdb64b0b9a47685d5/src/WaitHelpers/ExpectedConditions.cs#L141