perlweb-scrapingwww-mechanizewww-mechanize-firefox

How do you select specific HTML elements with no class or ID using WWW::Mechanize::Firefox?


I'm trying to loop through links on a page but i only want to loop through specific ones. The problem is that the links in <a> anchors do not have CSS ids or classes at all.

eg.

<a title="View More Information on FOO" href="tranlist.phtml?scode=FOO&sname=&refpg=1&snapcode=&ssector=1123&scheme=default" name="tranlist">

The only thing unique is the name but it doesn't seem like it can be specified with CSS selector.


Solution

  • From the CSS specification, a CSS selector like a.normal is the same as a[class~=normal], and you can match any attribute that way. So you need

    $mech->selector('a[name=tranlist]')