When one or more HTML elements are contained within an element, the element that contains the other elements is called the parent, and the contained element is a child of the parent. In our example, id="book" is the attribute of the element and class="book_name" is the attribute of the element. You can find the complete HTML attribute reference here. Some of the most common attributes are href, title, style, src, id, class, and many more. An attribute usually come in name/value pairs like: name="value". Here's a sample HTML we'll use to demonstrate.Īn attribute provides additional information about an element and is always specified in the start tag of the element. How to write an XPath (cheat sheet included)īefore we start writing an XPath, let's first cover some key terms. This short XPath would look for the element book regardless of its absolute location in the HTML, then go one level down to find the target element author.ģ. For example, the short XPath for /bookstore/book/author can be written as //book/author. The XPath that starts from the root element (the top element in the doc) and goes through all the elements in between to the target element is called an Absolute XPath.Įxample: "/html/body/div/div/div/div/div/div/div/div/div/span/span/span…"Ībsolute path can be long and confusing, so t o simplify Absolute XPath, we can use "//" to reference the element we want to start the XPath with (also known as a short XPath). With XPath, you can find the page elements quickly and easily just like finding a file on your computer. Look familiar?Įvery file on the computer has its own path, so are the elements on a web page. To find the file named "author", the exact file path is \bookstore\book\author. If you are having trouble understanding how it works, think about how we go about finding a particular file on our computer. For our example, if we want to locate the element "author", the XPath would be like: XPath uses "/" to connect tags of different levels from the top to the bottom in order to specify the location of an element. An HTML element usually consists of a start tag and an end tag, with the content inserted in between. Text with angle brackets( ) is called a tag. Title, author, year, price are all level 3. HTML has different levels of elements, just like a tree structure. In this example, Level 1 is bookstore and level 2 is book. It can be easily understood as the "path" to find the target element within the HTML doc. XPath is the most commonly used language when people need to locate an element in an HTML doc.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |