HTML元素yandex qa工具selenium包装器工作得很好,当元素在页面对象上时,使用@FindBy
注释和Page Object构造函数中的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();
}
在这种情况下,Element构造函数看起来像
public Element(By locator) {
this.locator = locator;
}
得到错误:no such element: Unable to locate element
即使我直接尝试init元素
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")
}
}