The find()
method will find all the descendant elements for the given element. This method is provided by the Mojo::DOM
module, which is an HTML/XML DOM parser with CSS selectors.
$dom->find('html element')
This method will return all the descendant elements as Mojo::Collection
where each element is a Mojo::DOM
object.
Let’s look at an example of this:
use 5.010;use Mojo::DOM;# Parse the htmlmy $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside paragraph </p><h1>Inside heading 1</h1></div>');# find descendants for divsay $dom->find('p')->map('tag')->join("\n");
In the code snippet above:
Mojo::DOM
module.$dom
.p
for the given HTML using the find
method and print all tag names present in the returned Mojo::Collection
.