Selenium 2.0 and findBy - Custom properties or Expando Properties

1.4k Views Asked by At

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());
1

There are 1 best solutions below

1
On

Sure. You can refer to any attribute of the element in either an XPath or CSS locator. For example:

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

or

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

For what it's worth, if your developers can put a custom property with a guaranteed-unique value on every interesting element, you should ask them why they won't put that value in the id= attribute instead. Because element IDs are special in HTML, DOM, and Selenium, and elements can be located much more quickly by their ID than by any other technique.