Can anyone please help me how to use contains in my xpath? My xpath changes all the time when users are added, so I can't find element using xpath.

This is my xpath:


Changes when new user added:


Please help me how to get it done.

  • Share you HTML and element you are trying to locate.
    – olyv
    Commented Nov 25, 2014 at 7:20

8 Answers 8


I have tested an ExtJS application. Most of the page element attributes are dynamic. They change not only when you add a new user or something, they change every time when you open the application.

I have found the xpath expressions I get from the tools (Firebug etc.) is not very useful. Here is why:

  1. hard to read
  2. break easily
  3. hard to debug

What I do instead, is rather to spend time looking at the HTML and identify possible unique attributes that are not dynamic and come up with my own expressions. If none of the attributes are static for a certain element, use any other element on the page that does have static attributes with the parent/child/sibling relations to locate.

I often use "contains", but there are more. Here are some examples:

  • multiple condition: //div[@class='bubble-title' and contains(text(), 'Cover')]
  • partial match: //span[contains(text(), 'Assign Rate')]
  • starts-with: //input[starts-with(@id,'reportcombo')]
  • value has spaces: //div[./div/div[normalize-space(.)='More Actions...']]
  • sibling: //td[.='LoadType']/following-sibling::td[1]/select"
  • more complex: //td[contains(normalize-space(@class), 'actualcell sajcell-row-lines saj-special x-grid-row-collapsed')]

Take a look at the W3C XSL Functions page for some more ideas.

Edit: Link updated. Edit 2: XPATH changed

  • Great examples! I'm using similar methods for automating a JSF app with a lot of dynamic forms. I've found the W3C specification very useful. Commented Feb 10, 2015 at 22:09
  • What a pleasure to know that someone out there has walked the same path! Thank you!
    – hxin
    Commented Feb 25, 2015 at 1:43
  • 1
    The previous W3C specification link is not working now. Please refer to this page instead.
    – hxin
    Commented Feb 6, 2017 at 22:26

You should be splitting the xpath to fetch individual tables for clarity sake.

I suggest the following:

// Get the content or container    
WebElement content = driver.findElement(By.id("contentText"));

//Get the table of users
WebElement tblUsers = content.findElement(By.xpath(".//table/tbody/tr[2]/td/table/tbody/tr/td[1]/table"));

// Get the rows which change always as and when users are added
WebElement allUsers = tblUsers.findElements(By.xpath(".//tbody/tr"));

// Loop through each row of users table
for(WebElement user : allUsers) {

   // Get the username
   WebElement username = user.findElement(By.xpath(".//td/table/tbody/tr/td[1]/strong[2]"));
   System.out.println("Username: " + username.getText());
  • 1
    this doesn't really provide any example on how to use contains Commented Feb 10, 2015 at 21:57

We can use something like

By.xpath("//td[contains(text(),'youruser')]") //here user text is case sensitive

By.xpath("//td[contains(lower-case(text()),'youruser')]") //to handle case sensitivity. Here user is not case sensitive


That XPath should be drastically shortened. That is likely to fail if you are testing in different browsers and if anything ever changes on the page it could throw a false positive due to it looking at the wrong element. I recommend looking up "Relative XPaths" and also "XPath Axes", but I could demonstrate a better XPath for you if you posted a screenshot of the HTML and the web page.

Based on what you posted, you could do something like: //table[@id ='something' or @class='Something that identifies this specific table']//tr[contains(text(), 'something to identify the row') or ./text() = 'Exact Text Match']//strong[2]

Usually with table rows I end up identifying the row based on the text from a cell within the row. //table[@id ='something' or @class='Something that identifies this specific table']//tr[.//td[contains(text(), 'something to identify the row') or ./text() = 'Exact Text Match']]//td//strong[2][contains(text(), 'Partial Text Match') or ./text() = 'Exact Text Match']


Please try below code:

public List<WebElement> getText(String xpath) throws Exception {
    return driver.findElements(By.xpath(xpath)).getText();

    system.out.println("Your text is available in the particular xpath");
} else {
    system.out.println("Your text is not available in the particular xpath");

It is working fine for me. I can find any text on any xpath using above code.


The 2 of your XPath changes from tr[9] to tr[10]. It seems that the number of the 3rd "tr" means user number.

In my practice delete that number can extract all users' data from HTML.(tested by RSelenium + rvest):


Although many answers are already present you can also refer the below links:


XPath CheatSheet








driver.findElement(By.xpath("//tag[@AN='AV' and @AN='AV']")).click();




ex : //input[starts-with(@id,'reportcombo')]

Not the answer you're looking for? Browse other questions tagged or ask your own question.