java - 如何在没有页面工厂(@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 元素包装器?
解决方案
没有足够的上下文来提供最佳解决方案,但这里有一个:将您的项目定义为元素数组。然后你可以遍历找到一个特定的:
@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")
}
}
推荐阅读
- android - 路由后,TextField 单击重建/重新加载小部件
- c# - 在 c# MVC 中从外部系统调用 Web API 时获取通知
- android - 如何在 android 的布局中自动调整我的项目位置?
- java - Spark 是否支持 BigInteger 类型?
- azure - 通过 Azure CLI 在资源创建阶段设置标签
- templates - 2 相同的文件,如何在 Vue.js 中构建模板/组件 2
- angular - 在 Angular 6 中找不到服务名称
- linux - 无法使用 Apache 通过 SSH 连接服务器
- c# - 绘制和保存图像
- php - 如果没有正确显示结果