in reply to No Elements Found with WWW::Mechanize::PhantomJS

At least when visiting the page from Firefox and right-clicking the element, I get a different XPath:

/html/body/div[4]/main/div/div[2]/div/div/div[1]/div[1]/div[2]

instead of your query:

/html/body/div[4]/main/div/div[2]/div/div/div[2]/div/div[1]/div[1]/div +[2]

But why are you using such a complex query when .tn-napsTable should work the same?

my $cont= $mech->selector('.tn-napsTable', one => 1);

Replies are listed 'Best First'.
Re^2: No Elements Found with WWW::Mechanize::PhantomJS
by rmperl (Novice) on Jun 04, 2018 at 10:36 UTC

    Thanks for this. Much appreciated. I've run the code with your suggestion but no matter what I do I keep getting the following error message when I use your suggested CSS selector:

    No elements found for CSS selector '.tn-napsTable' at /usr/local/share/perl/5.22.1/WWW/Mechanize/Plugin/Selector.pm

    And I get the error message: No elements found for /html/body/div4/main/div/div2/div/div/div1/div1/div2 when I run the xpath

    Do you have any ideas as to what might be going wrong? I'm at a loss. Thank you again

      Maybe what PhantomJS sees as HTML content is different from what Firefox sees. In such cases, I usually print out what ->content returns and wonder why that is different from what my browser shows me.

      I admit that most of my development on that family of modules is focused on WWW::Mechanize::Chrome currently, but I'll try to reproduce your case once I get to an environment where I have PhantomJS installed.

      This is really weird. For me, the below program works and outputs the HTML that I somewhat expect:

      use strict; use WWW::Mechanize::PhantomJS; use Data::Dumper; my $mech = WWW::Mechanize::PhantomJS->new( launch_arg => ['ghostdriver/src/main.js' ], ); $mech->get('https://www.racingpost.com/tipping/naps-table/'); sleep 5; my $cont= $mech->selector('.tn-napsTable', one => 1 ); my $content=$cont->get_attribute('innerHTML'); print $content; __END__ <div class="tn-napsTable__header"><div class="tn-napsTable__main"><div + role="but ton" tabindex="0" class="tn-napsTable__cell tn-napsTable__cell_header +tn-napsTab le__napsTipster_header"><!-- react-text: 15 -->Today's naps / Tipster< +!-- /react -text --></div><div role="button" tabindex="0" class="tn-napsTable__ce +ll tn-naps ...

      This is with PhantomJS v 2.1.1 on Windows and WWW::Mechanize::PhantomJS 0.18.

        Thank you so much for this. Works fine now that I've upgraded PhantomJS to v2.1.1. I was running the code before using an earlier version of PhantomJS v1.9 on Ubuntu 16.04