FindBy属性何时触发driver.FindElement? [英] when do FindBy attributes trigger a driver.FindElement?
问题描述
我的问题是:装饰有findby属性的Webelements是否在每次引用它们时调用findelement函数?如果没有,什么时候?
My question is: do webelements decorated with findby attributes call the findelement function upon each reference to them? If not, when?
使用List<的过程是什么? webelement>哪个也装饰过?当您引用列表或引用列表中的元素时,它会触发吗?
And what is the procedure with List< webelement > which is also decorated? Does it trigger when you reference the list, or when you reference an element inside that list?
我之所以问是因为,在某些情况下,我会遇到过时的元素异常,并且我想知道如何处理它们.
I'm asking because I have some situations where I'm getting stale element exceptions and I want to know how to deal with them.
推荐答案
WebElements的计算是惰性的.也就是说,如果您从不在PageObject中使用WebElement字段,则永远不会调用它的"findElement". 参考.
WebElements are evaluated lazily. That is, if you never use a WebElement field in a PageObject, there will never be a call to "findElement" for it. Reference.
如果不想每次WebDriver
都查询元素,则必须使用@CacheLookup
批注.
If don't want WebDriver
to query the element each time, you have to use the @CacheLookup
annotation.
我的问题的列表部分如何?
What about the list part of my question?
当您从列表中查询时,将触发findElements.说你有:
The findElements is triggered when you query from the list. Say you have:
@FindBy(xpath = "//div[@class=\"langlist langlist-large\"]//a")
private List<WebElement> list;
下面的代码对所有 trigger 的findElements进行采样:
Following code samples all trigger the findElements :
list.isEmpty();
WebElement element = list.get(0);
为
List<WebElement> newList = new ArrayList<WebElement>();
newList = list;
不会触发.
请检查LocatingElementListHandler
类.我建议深入探讨答案.
Please check the LocatingElementListHandler
class. I suggest diving into the source for answers.
您可能会发现PageFactory类中的以下代码注释很有帮助:
You may find this code comment from PageFactory class helpful:
/**
* Instantiate an instance of the given class, and set a lazy proxy for each of the WebElement
* and List<WebElement> fields that have been declared, assuming that the field name is also
* the HTML element's "id" or "name". This means that for the class:
*
* <code>
* public class Page {
* private WebElement submit;
* }
* </code>
*
* there will be an element that can be located using the xpath expression "//*[@id='submit']" or
* "//*[@name='submit']"
*
* By default, the element or the list is looked up each and every time a method is called upon it.
* To change this behaviour, simply annotate the field with the {@link CacheLookup}.
* To change how the element is located, use the {@link FindBy} annotation.
*
* This method will attempt to instantiate the class given to it, preferably using a constructor
* which takes a WebDriver instance as its only argument or falling back on a no-arg constructor.
* An exception will be thrown if the class cannot be instantiated.
*
* @see FindBy
* @see CacheLookup
* @param driver The driver that will be used to look up the elements
* @param pageClassToProxy A class which will be initialised.
* @return An instantiated instance of the class with WebElement and List<WebElement> fields proxied
*/
这篇关于FindBy属性何时触发driver.FindElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!