Selenium FindBy属性何时触发driver.FindElement?
我的问题是:用findby属性修饰的webelements是否在每次引用它们时调用findelement函数?如果没有,什么时候 ListSelenium FindBy属性何时触发driver.FindElement?,selenium,selenium-webdriver,webdriver,pageobjects,Selenium,Selenium Webdriver,Webdriver,Pageobjects,我的问题是:用findby属性修饰的webelements是否在每次引用它们时调用findelement函数?如果没有,什么时候 List的过程是什么,它也是修饰的?它是在引用列表时触发的,还是在引用列表中的元素时触发的 我这样问是因为我遇到了一些情况,在这些情况下我会遇到过时的元素异常,我想知道如何处理它们。WebElements是惰性地评估的。也就是说,如果您从未在PageObject中使用WebElement字段,则永远不会为其调用“findElement” 如果不希望WebDriver每
我这样问是因为我遇到了一些情况,在这些情况下我会遇到过时的元素异常,我想知道如何处理它们。WebElements是惰性地评估的。也就是说,如果您从未在PageObject中使用WebElement字段,则永远不会为其调用“findElement” 如果不希望
WebDriver
每次查询元素,则必须使用@CacheLookup
注释
那我问题的列表部分呢 当您从列表中查询时,会触发findElements。假设你有:
@FindBy(xpath = "//div[@class=\"langlist langlist-large\"]//a")
private List<WebElement> list;
WebElement=list.get(0)代码>
何处为
List<WebElement> newList = new ArrayList<WebElement>();
newList = list;
对于问题1),页面工厂模式中的概念是仅在任何操作中使用WebElement时才识别WebElement
对于问题2),无论何时尝试访问页面类变量(WebElement或List)@FindBy都会根据变量类型触发FindElement或FindElement
class LoginPage{
.....
@FindBy(id, "uname")
WebElement username;// no trigger
@FindBy(xpath, "//table/tr")
List<WebElement> pdtTable; // no trigger
.....
.....
public void enterUserame(String text){
uname.sendKeys(text);
}
.....
.....
}
.....
.....
LoginPage loginPage = PageFactory
.initElements(driver, LoginPage.class); // creates WebElement variables but not triggers
if(loginPage.uname.isDisplayed()){// Trigger happens
loginPage.enterUserame("example");// Trigger happens
}
int count=pdtTable.size();// Trigger happens for FindElements
类登录页{
.....
@FindBy(id,“uname”)
WebElement用户名;//无触发器
@FindBy(xpath,“//表/tr”)
List pdtTable;//无触发器
.....
.....
公共无效EnterUserName(字符串文本){
uname.sendKeys(文本);
}
.....
.....
}
.....
.....
LoginPage LoginPage=PageFactory
.initElements(驱动程序,LoginPage.class);//创建WebElement变量,但不创建触发器
如果(loginPage.uname.isDisplayed()){//触发器发生
loginPage.enterUserName(“示例”);//触发器发生
}
int count=pdtTable.size();//FindElements发生触发器
其他信息:PageFactory annotation@CacheLookup用于标记定位后的WebElements,以便始终可以使用DOM中的同一实例。此注释应用于WebElement时,指示Selenium保留WebElement的缓存,而不是每次从网页中搜索WebElement。这有助于我们节省大量时间。我的问题的列表部分如何?因此,当调用方法时,它会触发它。谢谢那么:webelement x=list[0]呢。在这种情况下,它是否会触发列表上的findelements?AFAIK list没有[]运算符,但如果有,则会调用findelements。
/**
* 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
*/
class LoginPage{
.....
@FindBy(id, "uname")
WebElement username;// no trigger
@FindBy(xpath, "//table/tr")
List<WebElement> pdtTable; // no trigger
.....
.....
public void enterUserame(String text){
uname.sendKeys(text);
}
.....
.....
}
.....
.....
LoginPage loginPage = PageFactory
.initElements(driver, LoginPage.class); // creates WebElement variables but not triggers
if(loginPage.uname.isDisplayed()){// Trigger happens
loginPage.enterUserame("example");// Trigger happens
}
int count=pdtTable.size();// Trigger happens for FindElements