How can I use the “contains” method for locating a dynamic element on a web page?
I am currently assigned a particular task which is related to locating a dynamic element on a web page. How can I do it by using the method called “contains” in the whole context of selenium considering where the values related to the attribute would match a changing substring?
In the context of selenium, when you are dealing with elements where values related to an attribute contain changing substring, then you can use the “contains in selenium” method in a CSS selector for creating a locator which would be flexible. Here are the steps given for how you can do so:-
Identify the common attribute
First, you would need to identify a part of the attribute values that helps in remaining constant or even predictable.
Use CONTAINS in the CSS selector
Now you can create a CSS selector with the method “contains” for matching the dynamic substring.
WebElement dynamicElement = driver.findElement(By.cssSelector(“[attribute*=’partialValue’]”));
Use “contains” in Xpath
You can use the “contains” in X path for the same purpose:-
WebElement dynamicElement = driver.findElement(By.xpath(“//*[contains(@attribute, ‘partialValue’)]”));