Is using DOM tags in xpath locators a good practice?

batuarslan :

In my test cases, more than often I'm using XPath locators that check text attributes. For example,

public By label(String text) {
    return By.xpath("//label[contains(text(),'"+text+"')]");
}

and in test cases I use it like:

webUI.verifyElementVisible(page.label("message"));

I am doing this with labels, divs, inputs (placeholder), spans, etc.

Is this considered good practice?

What would be the downsides of doing this in the future?

This greatly shortens my development time, makes my page object repositories much cleaner and concise.

TheSociety :

Key Point : CSS selector is highly recommended over XPath due to its simplicity, speed, and performance.

Remember : Xpath engines are different in each browser, hence make them inconsistent.

Specially, IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Hence we lose the advantage of using native browser features that WebDriver inherently promotes.

Hence, larger audience would recommend to use CSS over Xpath.

Example : CSS Selector for a label (text): label[for=city]

<label for="city">New York</label> 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116143&siteId=1
Recommended