0

Anyone have a suggestion for finding elements on a page by a custom property, like an expando property?

Problem: Site uses alot of JSP and dynamic images for buttons. They do not have static names or IDs. (Think of a registration or checkout process where every page has a "next" or "continue" button, but it dynamically takes you somewhere depending upon current context)

Suggested Solution: My repeated requests that these are not automate-able have recently been answered with a custom expando property of "btn-tag-title".

If every element on a page has a unique (but custom) property named btn-tag-title=, can selenium 2.0 find it reliable every time?

Other Suggested Solutions?

    log.info(driver.getCurrentUrl());
    assertTrue(selenium.isElementPresent("btn-tag-title=Sign In"));
    selenium.type("name=username", "demo");
    selenium.type("name=password", "tester");
    selenium.click("btn-tag-title=Sign In");
    log.info(driver.getCurrentUrl());
4

1 回答 1

0

当然。您可以在 XPath 或 CSS 定位器中引用元素的任何属性。例如:

assertTrue(selenium.isElementPresent("//*[@btn-tag-title='Sign In']"));
...
selenium.click("//*[@btn-tag-title='Sign In']");

或者

assertTrue(selenium.isElementPresent("css=*[btn-tag-title='Sign In']"));
...
selenium.click("css=*[btn-tag-title='Sign In']");

对于它的价值,如果您的开发人员可以在每个有趣的元素上放置一个具有保证唯一值的自定义属性,您应该问他们为什么他们不会将该值放入id=属性中。因为元素 ID 在 HTML、DOM 和 Selenium 中是特殊的,并且通过 ID 可以比任何其他技术更快地定位元素。

于 2011-11-18T18:10:24.763 回答