Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. QA Testing
  3. Question
QA Testing

How can I locate elements using the xpath contains class in selenium?

Asked by Ananya Pawar May 9, 2022 6.2K views 2 answers
Share

About this question

 I have below two input fields and I have to find their location I can't use class selector cause iI have to identify element based on formcontrolname and ng-invalid class



<input _ngcontent-thn-c28="" formcontrolname="nic_no" igxinput="" type="text" placeholder="00000-0000000-0" class="ng-untouched ng-pristine ng-invalid igx-input-group__input">


<input _ngcontent-thn-c28="" formcontrolname="username" igxinput="" type="text" placeholder="UserName" class="ng-untouched ng-pristine ng-invalid igx-input-group__input">

Therefore I'm using xpath. Below is what I did:
driver.findElement(By.xpath("//div/input[(@formcontrolname='nic_no') and (@class='ng-invalid')]"); 


driver.findElement(By.xpath("//div/input[(@formcontrolname='nic_no') and (@class='ng-invalid')]"); 
But I'm getting error apparently I have to write all classes name inside @class, like this:
driver.findElement(By.xpath("//div/input[(@formcontrolname='nic_no') and (@class='ng-untouched ng-pristine ng-invalid igx-input-group__input')]"); 

If I do like above then I won't get errors but I need to use only one class name, cause there are changes more classes may include and reduce from this list of classes.


Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jun 6, 2024

o locate elements using the XPath contains function with a class attribute in Selenium, follow these steps:

1. Understanding XPath contains Function

The contains function in XPath is useful for finding elements whose attributes include a specific value. This is especially helpful for class attributes, as elements can have multiple classes.

2. Basic XPath Syntax

The syntax for using contains with a class attribute in XPath is:

  //tag[contains(@class, 'class_name')]

Here:

  //tag is the tag name of the HTML element you are looking for (e.g., div, span, a).

@class specifies that you are looking for the class attribute.

'class_name' is the class name you are searching for.

3. Using Selenium to Locate Elements

Here's how you can use this XPath in a Selenium script:

Example in Python:

from selenium import webdriver

  # Initialize the WebDriver (assuming you have the chromedriver in your PATH)driver = webdriver.Chrome()# Open a webpagedriver.get('http://example.com')# Locate an element using XPath with contains and classelement = driver.find_element_by_xpath("//*[contains(@class, 'class_name')]")# Perform actions with the located elementprint(element.text)# Close the WebDriverdriver.quit()Example in Java:javaCopy codeimport org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class LocateElementExample {    public static void main(String[] args) {        // Initialize the WebDriver (assuming you have the chromedriver in your PATH)        WebDriver driver = new ChromeDriver();        // Open a webpage        driver.get("http://example.com");        // Locate an element using XPath with contains and class        WebElement element = driver.findElement(By.xpath("//*[contains(@class, 'class_name')]"));        // Perform actions with the located element        System.out.println(element.getText());        // Close the WebDriver        driver.quit();    }}

4. Advanced Usage

If you need to be more specific (e.g., selecting a div with a specific class), you can modify the XPath accordingly:

  //div[contains(@class, 'class_name')]

  • Ensure the class name is correct: Verify that the class name you are using in your XPath exists in the elements you are trying to locate.
  • Check for typos: Ensure there are no typos in your XPath expression.
  • Browser compatibility: Ensure your WebDriver (e.g., ChromeDriver, GeckoDriver) is compatible with the browser version you are using.

By following these steps, you should be able to locate elements using the XPath contains function with a class attribute in Selenium.









Was this helpful?

More QA Testing discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest QA Testing Blogs

Guides, tips and career advice on QA Testing from JanBask experts.