#6.0 What XPath: Common Patterns

Souvik DeySouvik Dey
3 min read

In web automation, certain XPath patterns are frequently used to interact with common web elements. This section will cover practical XPath techniques for locating and interacting with buttons, links, forms, input fields, tables, and lists.

Buttons

  1. By exact text:

     //button[text()='Submit']
    
  2. By partial text:

     //button[contains(text(), 'Submit')]
    
  3. By title attribute:

     //button[@title='Submit Form']
    
  4. By class:

     //button[contains(@class, 'submit-btn')]
    
  5. Image buttons:

     //input[@type='image' and @alt='Submit']
    
  1. By exact text:

     //a[text()='Home']
    
  2. By partial text:

     //a[contains(text(), 'Account')]
    
  3. By href attribute:

     //a[contains(@href, 'account/settings')]
    
  4. By title attribute:

     //a[@title='Go to Homepage']
    
  5. Combining attributes:

     //a[@href='/login' and contains(@class, 'nav-link')]
    

6.2 Working with Forms and Input Fields

Form Elements

  1. Locating a form by ID:

     //form[@id='login-form']
    
  2. Finding inputs within a specific form:

     //form[@id='login-form']//input
    

Input Fields

  1. By name attribute:

     //input[@name='username']
    
  2. By placeholder text:

     //input[@placeholder='Enter your email']
    
  3. By label text (assuming proper HTML structure):

     //label[text()='Email:']/following-sibling::input
    
  4. Checkbox by label:

     //label[contains(text(), 'Remember me')]/input[@type='checkbox']
    
  5. Radio button by value:

     //input[@type='radio' and @value='male']
    

Select Dropdowns

  1. Locating a select element:

     //select[@name='country']
    
  2. Finding specific options:

     //select[@name='country']/option[text()='United States']
    
  3. Selecting by value attribute:

     //select[@name='country']/option[@value='US']
    

6.3 Navigating Tables and Lists

Tables

  1. Selecting a specific cell by row and column index:

     //table[@id='data-table']/tbody/tr[2]/td[3]
    
  2. Finding a row containing specific text:

     //table[@id='data-table']//tr[contains(., 'Specific Text')]
    
  3. Selecting a cell based on column header:

     //table[@id='data-table']//th[text()='Name']/following-sibling::td
    
  4. Selecting the last row of a table:

     //table[@id='data-table']/tbody/tr[last()]
    
  5. Finding a cell with specific text and getting its row index:

     count(//table[@id='data-table']//td[text()='Target Text']/parent::tr/preceding-sibling::tr) + 1
    

Lists

  1. Selecting all list items:

     //ul[@class='menu-items']/li
    
  2. Finding a specific list item by text:

     //ul[@class='menu-items']/li[text()='About Us']
    
  3. Selecting the first item of a list:

     (//ul[@class='menu-items']/li)[1]
    
  4. Selecting items after a specific item:

     //li[text()='Target Item']/following-sibling::li
    
  5. Selecting nested list items:

     //ul[@class='parent-menu']//li
    

6.4 Handling Dynamic Content

  1. Elements with dynamic IDs (using starts-with):

     //div[starts-with(@id, 'message-')]
    
  2. Finding elements with multiple possible classes:

     //div[contains(@class, 'message') or contains(@class, 'notification')]
    
  3. Locating elements that appear after AJAX calls (using wait conditions):

     //div[@id='result' and contains(@style, 'display: block')]
    

6.5 Best Practices

  1. Use specific attributes when possible (id, name, etc.) for faster and more reliable locators.

  2. Avoid using indexes unless necessary, as they can break easily if the page structure changes.

  3. Use contains() for classes when dealing with dynamic or multiple classes.

  4. Combine multiple attributes or conditions to create more robust locators.

  5. When working with tables, try to use relationships between elements (like headers to data cells) rather than hard-coded indexes.

  6. For forms, utilize the relationship between labels and input fields when possible.

  7. Always consider the possibility of dynamic content and use appropriate wait strategies in your automation scripts.

Remember, while these XPath patterns are common and useful, the best XPath for your specific use case will depend on the structure of the website you're automating. Always analyze the HTML structure and choose the most reliable and maintainable locator strategy.

0
Subscribe to my newsletter

Read articles from Souvik Dey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Souvik Dey
Souvik Dey

I design and develop programmatic solutions for Problem-Solving.