Using PageObjects, Page Factory and WebDriverWait in Selenium WebDriver using Java

隐身守侯 提交于 2019-11-29 04:36:56

The WebElements in a PageFactroy are really proxies to WebElements. This means that every time you access a WebElement it will perform a search to find the element on the page.

This has some advantages:

  • When the PageFactory is initialised the proxies are configured, but the WebElements are not found at that point (so you won't get a NoSuchElementException)
  • Every time you use a WebElement it will go and find it again so you shouldn't se StaleElementException's

You are using the @CacheLookup annotation which is losing you the second benefit as it will find the element once and then keep a reference to it, you are now far more likely to see StaleElementExceptions.

That being said you still keep the main benefit which is that Selenium will not go to the page and actually find the element until you first use it.

So in summary all you need to do is move

PageFactory.initElements(driver, this);

Into your constructor and it will all work fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!