📅  最后修改于: 2023-12-03 15:35:47.772000             🧑  作者: Mango
Xpath is a language used to query and select elements from an XML or HTML document. Xpath text is an Xpath expression used to select elements based on their text content. In this tutorial, we will explain how to use Xpath text to select elements from an HTML document.
//*[text()='<text_content>']
//*[contains(text(),'<partial_text_content>')]
Let's consider the following HTML snippet:
<!DOCTYPE html>
<html>
<head>
<title>Sample page</title>
</head>
<body>
<h1>Hello World</h1>
<p>This is a sample page for Xpath text.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
To select the element with the exact text content "Hello World", we can use the following Xpath text query:
//*[text()='Hello World']
This will select the h1
element.
To select all the li
elements that contain the text "Item", we can use the following Xpath text query:
//*[contains(text(),'Item')]
This will select all the li
elements.
Xpath text is a powerful tool for selecting elements based on their text content. With the text()
and contains()
functions, we can select elements that contain exact or partial matching text content. This makes it easy to locate and manipulate elements on an HTML page.