In Selenium with Java, the contains() function in XPath is useful when you want to locate an element based on a partial match of its attribute value.
I have copied the following HTML code from the link- https://en.wikipedia.org/w/index.php?title=Main_Page&action=history
<p>For any version listed below, click on its date to view it. For more help, see <a href="/wiki/Help:Page_history" title="Help:Page history">Help:Page history</a> and <a href="/wiki/Help:Edit_summary" title="Help:Edit summary">Help:Edit summary</a>. <span class="nowrap">(cur) = difference</span> from current version, <span class="nowrap">(prev) = difference</span> from preceding version, <span class="nowrap"><b>m</b> = <a href="/wiki/Help:Minor_edit" title="Help:Minor edit">minor edit</a></span>, <span class="nowrap">→ = <a href="/wiki/Help:Section#Section_editing" title="Help:Section">section edit</a></span>, <span class="nowrap">← = <a href="/wiki/Help:Automatic_edit_summaries" title="Help:Automatic edit summaries">automatic edit summary</a></span></p>
There are multiple title attributes with the value containing the word ‘Help’. Here I have located those title attributes which have the word Help in their values and then extracted the text using getText() method of java.
driver.navigate().to("https://en.wikipedia.org/w/index.php?title=Main_Page&action=history");
List<WebElement> elemH=driver.findElements(By.xpath("//*[contains(@title,'Help')]"));
for (WebElement elh : elemH) {
System.out.println(elh.getText()
};
In the example above, //*[contains(@title, ‘Help’)] is the XPath expression. It finds any element on the page (*) where the title attribute contains the text “Help”.
Syntax :-
driver.findElement(By.xpath(“//*[contains(@attribute,’value’)]”))