Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: WWW::Mechanize fetch differs from firefox browser result (was Re: WWW::Mechanize or WWW::Selenium with javascript redirect)

by tphyahoo (Vicar)
on Jan 18, 2007 at 18:53 UTC ( #595311=note: print w/ replies, xml ) Need Help??


in reply to WWW::Mechanize fetch differs from firefox browser result (was Re: WWW::Mechanize or WWW::Selenium with javascript redirect)
in thread WWW::Mechanize or WWW::Selenium with javascript redirect

Okay, I bullied a coworker into letting me use their windows box, and tried out Win32::IE::Mechanize. Unfortunately, I don't think this is going to work either. But I learned some things.

Reason it won't work: "The internet Explorer automation object does not provide an interface to popup windows generated by security settings or Jscript contained on the page" (from the docu).

Unless I am mistaken, when I try to open my target page, there is exactly this: a javascript redirect. The effect of my test program (below) is that an empty IE window, with a blank browser bar, gets opened, and separately a second window is opened with the html that I want. But as the docu says, there is no way to get at the second window. (Since the window that I am able to get html from appears to be the blank one.) Unless there is some way to get around this "no way to access popup windows" issue with OLE or something (and I know nothing about OLE) I guess this is a dead end.

I was next going to try out Mozilla::Mechanize, but I read in the docu for that that it was based on IE::Mechanize, and the docu doesn't even mention popups, so I'm going to save it for last. Finally there is Selenium. Selenium has a function called

$sel->get_all_window_ids() Returns the IDs of all windows that the browser knows about.
That seems promising. As I said above, I had troubles getting selenium to work with perl on linux. Maybe I'll have better luck on windows. I'll report my findings when they are in.

Finally, FWIW, here's my experimentations with mechanize, and my failed attempts to work around the popup window.

use strict; use warnings; use Win32::IE::Mechanize; my $ie = Win32::IE::Mechanize->new( visible => 1 ); use Data::Dumper; my $price = '249,9'; print "price: $price\n"; my $url = 'http://xml.pangora.com/scripts/Redirect.php?fid=45&mid=1066 +&serviceName=idealo-de&serviceType=portal&oid=1066de515358&sid=73&pt= +idealo-de.export.1-0&url=http%3A%2F%2Fwww.baur.de%2Fis-bin%2FINTERSHO +P.enfinity%2FWFS%2FBaur-BaurDe-Site%2Fde_DE%2F-%2FEUR%2FBV_ExternalCa +ll-Start%3FArticleNo%3D515358%26NUMSArt%3D4443504%26NUMSArtPc%3D44886 +15%26AffiliateID%3Dpangora-%2A%26Name%3Dpangora-produktdaten-baur%26A +ctionID%3Dpreis-produkt-suche-baur%26WKZ%3D79%26IWL%3D101'; # if visible, you get an open IE window. my $mech = Win32::IE::Mechanize->new( visible => 1 ); #$mech->agent('Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.8.1) Gecko +/20061010 (IKDhPmJcdw) Firefox/2.0'); $mech->get( $url ); my $html = $mech->content; unless ( $html =~ /$price/ ) { print "html from $url doesn't match $price\n"; print "even though IE mech opened a visible IE window, and the text +there obviously DOES match.\n"; print "and what the heck is this blank IE window with no url in the +location bar?\n"; print "you don't get that extra blank window if you call IE Mech aga +inst a more normal looking url, like http://www.yahoo.com.\n"; print "aaaaghh!\n"; # uncomment this to print html, which is totally different from what + you get from firefox, show source. # print "html: $html"; } # # need something like # my $ieWindows = getwindows(); # should be two windows. # but ... module docu says "The internet Explorer automation object do +es not provide # an interface to poupp windows generated by security settings or jscr +ipt contained in the page". # So, I guess this won't work. Unless I can get into the IE object wit +h OLE or something, # (which I know nothing about).


Comment on Re: WWW::Mechanize fetch differs from firefox browser result (was Re: WWW::Mechanize or WWW::Selenium with javascript redirect)
Select or Download Code
Re^2: WWW::Mechanize fetch differs from firefox browser result (was Re: WWW::Mechanize or WWW::Selenium with javascript redirect)
by perrin (Chancellor) on Jan 18, 2007 at 22:19 UTC
    Seriously, there is no need to use Selenium or IE. If you use a logging proxy or the Firefox LiveHTTPHeaders extension, you will see exactly what the browser sends to the server, and then you just have to make your script send the same thing. No actual execution of JavaScript is required, since the server has no way to tell if you ran any client-side code or not.
      I am going to try out LiveHTTPHeaders; thanks for that suggestion. I think partly my difficulties are just due to my general ignorance of javascript.

      However, I still see value value in figuring out a more general way to do it, that susses out any popup windows and checks them, regardless of how they were generated. If I can get that to work, I won't have to change the code even if something changes in the javascript.

        I'm also trying LiveHTTPHeaders, the output is a little confusing though. Question: 1. Many HTTP lines are passed. 2. Do I need to create a new mech object for each "HTTP" that I captured? Has anyone had any success yet?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://595311]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-07-26 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls