gpt4 book ai didi

java - "PageFactory.initElements(driver, this);"如何处理WebElements列表?

转载 作者:行者123 更新时间:2023-11-30 06:08:27 25 4
gpt4 key购买 nike

我只是想知道 PageFactory.initElements(driver, this) 的工作原理,它如何填充 searchSuggestions webElement 并且如果我不使用 thread.sleep(500) ,它会使用 thread.sleep 抛出陈旧元素引用异常工作正常。

public SearchObjects(WebDriver driver) 
{
this.driver = driver;
PageFactory.initElements(driver, this);
}
@FindBy(id="navbar-query")
WebElement searchBar;

@FindBy(xpath="//div[@class='navbar-suggestionsearch__search-result']")
List<WebElement> searchSuggestions;

public SearchObjects(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void searchOnImdb(WebDriver driver, String str) throws InterruptedException
{
searchBar.sendKeys(str);
Thread.sleep(500);
}

最佳答案

PageFactory.initElements()只为标有注释的变量创建代理@FindBy 。该方法实际上并不搜索这些元素。对于WebElementWebElement 的代理List<WebElement> 的创建方式类似.

引用https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/PageFactory.java方法initElements()它调用方法 proxyFields() 。在此方法中 decorate()方法Decorator称为 - https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/pagefactory/DefaultFieldDecorator.java 。在decorate()方法中,您将发现使用各自的InnvocationHandler创建各自的代理。 。 InnvocationHandler处理元素等的实际搜索。

现在当类似 driver.findElements() 的时候使用时,它首先转到调用实际方法的代理。 -https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/pagefactory/internal/LocatingElementListHandler.javahttps://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/pagefactory/internal/LocatingElementHandler.java

尝试使用 ExpectedConditions 显式等待-https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

关于java - "PageFactory.initElements(driver, this);"如何处理WebElements列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50771820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com