简体   繁体   中英

How to use HTML elements yandex qa tools selenium wrapper without page factory (@FindBy using) but in dynamic pages

HTML elements yandex qa tools selenium wrapper works well and init elements when elements are on page object inited with @FindBy annotation and HtmlElementLoader.populatePageObject(this, ((WebDriverBrowser)Browser.getDriver()).getWebDriver()); in Page Object constructor

But if page content changes dynamically I want to create HtmlElement on the way without @FindBy lake for example:

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

In this case Element constructor looks like

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

Got error: no such element: Unable to locate element

And even if I directly try to init element

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

Got error

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

Is it possible to use Html elements wrapper like this?

Try

  WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(navigatorXpath)));

There is not enough context to offer a best solution, but here is one: define your items as an array of elements. Then you could iterate through to find a specific one:

@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")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM