HTML 元素 yandex qa 工具 Selenium 包装器工作得很好,当元素在页面上时,初始化元素 对象在页面对象中启动@FindBy
注释和HtmlElementLoader.populatePageObject(this, ((WebDriverBrowser)Browser.getDriver()).getWebDriver());
页面对象构造函数
但是,如果页面内容动态更改,我想在没有湖的情况下创建 HtmlElement@FindBy例如:
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 元素包装器?
试试
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(navigatorXpath)));
没有足够的上下文来提供最佳解决方案,但这里有一个:将项目定义为元素数组。然后,您可以遍历以查找特定的:
@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")
}
}