This is the first day of my participation in the Writing Challenge. See the writing Challenge for more details

Xpath is a very efficient way to get elements when you’re crawling or automating a browser. Xpath is a language for finding information in an XML document. XPath can be used to traverse elements and attributes in AN XML document. The simple idea is that xpath looks for specific elements in an XML document based on node attributes. HTML and XML are both markup languages, based on text editing and modification. So xpath can also be used to get information in THE HTML language

Xpath syntax one forward slash (/) and two forward slashes (//)

A slash ‘/’ when retrieving an element represents starting at the root of the entire document

For example/HTML /body/div/ul/li[1] this [1] corresponds to the subscript of the element from the array

The two slashes represent the start from the current node matched in the document

For example, “//div[@class = ‘table-con’]/ul/li[3]/input” instead of starting with the root HTML

The ‘@’ expression represents selection properties such as href class ID

Example: Here is my xpath fetch in the project. Get the node of an inPut box to operate on first find its parent node to see if it is unique. If unique uses relative paths directly // to start xpath syntax validation on the console

Start Stop Snipaste_2021-06-18_10-15-21.png

Contains () : //div[contains(@id,’in’)] Since the text value of a node is not an attribute, such as ‘baidu’, use the text() function to match the node: //a[text()=’baidu’]. Contains [contains(text(),’ baidu ‘)] starts-with() : //div[starts-with(@id,’in’)] //input[@name= ‘identity’ and not(contains(@class, ‘a’))], indicates that an input node whose name is identity and class does not contain a is matched. //input[@id] //input[not(@id)] //input[@id]

Xpath is divided into absolute paths and relative paths. Absolute paths: start with HTML and work your way up to the target element relative paths: Start with an element that is easily located, use that element as the base position, and then use the path method to find the target element

Xpath syntax structure //*[@id=”kw”] // indicates that the search starts from the path * indicates that any element [] the parentheses indicate the filter criteria @ indicates that the search is based on attributes e.g. @id=”kw” indicates that the search is based on an element whose ID attribute is kW

1. From the Google Developer tools, Element page, use CMD + F to open the search bar, which supports Xpath syntax 2. The element can also be located via the concle interface using $(‘xpath’) + Enter

$x(‘//input[@id=”kw”]’)

[put#kw.s_ipt] copy code Xpath location method 1.a tag with text text

Capture the 2021-05-02 22.41.54. PNG

//*[@id=”kw”] @id=” id “; //*[@id=”kw”] @id=” id “;

3. When looking for a dynamic element, reverse the path to the target element by the static element of the parent element or the child element note /.. You can find the parent of the relative element

Capture the 2021-05-02 22.58.52. PNG

The article classification