首页 > 解决方案 > 如何在没有页面工厂(@FindBy 使用)但在动态页面中使用 HTML 元素 yandex qa 工具 selenium 包装器

问题描述

HTML 元素 yandex qa 工具 selenium 包装器运行良好,并且当元素位于使用@FindBy注释初始化的页面对象和HtmlElementLoader.populatePageObject(this, ((WebDriverBrowser)Browser.getDriver()).getWebDriver());页面对象构造函数中时,初始化元素

但是,如果页面内容动态变化,我想在没有 @FindBy 湖的情况下创建 HtmlElement,例如:

public static void openNavigator(String navigatorName) {
    String navigatorPath = String.format(MENU_OBJ_XPATH_PATTERN, navigatorItemMenuName);
    Element navigatorMenu = new Element(By.xpath(navigatorXpath));
    navigatorMenu.waitForVisible();
}

在这种情况下,元素构造函数看起来像

public Element(By locator) {        
    this.locator = locator;
}

得到错误:no such element: Unable to locate element

即使我直接尝试初始化元素

HtmlElementLoader.createHtmlElement(Element.class, Browser.getBrowser().getWebDriver().findElement(By.xpath("//td[contains(text(),'Navigator')]")))

出现错误

no such element: Unable to locate element: {"method":"xpath","selector":"//td[contains(@id,'cnt-start')]//*[contains(text(),'Navigator')]"}
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

是否可以像这样使用 Html 元素包装器?

标签: javaselenium-webdriverautomationhtmlelements

解决方案


没有足够的上下文来提供最佳解决方案,但这里有一个:将您的项目定义为元素数组。然后你可以遍历找到一个特定的:

@FindBy(xpath = "./menu_locator")
public class SiteMenu extends HtmlElement {
    @FindBy(xpath = "./generic_item_locator")
    List<HtmlElement> items

    public clickOn(String navigatorName) {
        for (HtmlElement item: items) {
            if (item.getText() == "navigatorName") {
                item.click();
            }
        }
        throw new ElementNotFoundException("Navigator item not found")
    }
}

推荐阅读