Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: WWW::Mechanize::Firefox -- how to close firefox browser window

by Corion (Pope)
on Apr 29, 2013 at 07:23 UTC ( #1031172=note: print w/ replies, xml ) Need Help??


in reply to WWW::Mechanize::Firefox -- how to close firefox browser window

I would try to find the newly opened window, either through $mech->application->openTabs() or by finding the opened Window from within the opening window, depending on whether Javascript is used to open that window or not.

Then, closing that window is a simple matter of calling the ->close method on it:

$window->close(); # this is window.close() from Javascript


Comment on Re: WWW::Mechanize::Firefox -- how to close firefox browser window
Select or Download Code
Re^2: WWW::Mechanize::Firefox -- how to close firefox browser window
by jbernest (Novice) on Apr 30, 2013 at 22:38 UTC

    Thanks, Corion. Can I ask you another question? I can't figure out how to make a particular selection. I know JavaScript is involved on this page, but that's about all I know. I would love a quick answer, but I also would like to find resources to help me better understand the nuts and bolts. If you run this script (remember to have MozRepl 1.1 running), the selection I want is "Saccharomyces cerevisiae(25)", which is on the left side of the page in the scrollbar. It would work equally well for me to select this option by a name or label, or by its position, being the second selection in the list. In the page source, I can see the relevant section (involving a JS function "document.createElement"), but I don't know how to access these elements. I would greatly appreciate help with a solution, and/or simply some advice on resources to help me figure out a solution.

    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(autoclose => 0); $mech->allow( javascript => 1 ); $mech->get("http://david.abcc.ncifcrf.gov/summary.jsp"); $mech->click({ xpath => ('//*[@href="Upload a Gene List or Population" +]', single => 1), synchronize => 0 }); $mech->form_name('frmManager'); $mech->set_fields( 'pasteBox' => "RRI1, YLR149C, FUN19, YBR285W, ALD3, CUE5, RTC3 +, AIM3, YDL199C, FMP45, AST2, GIP2, YKL091C, YKL133C, GLG2, SYM1, VHS3, YJR124C, MCH1, MHO1, I +KS1, RKM1, STF2, SAC1, ATG7, YAK1", 'Identifier' => "OFFICIAL_GENE_SYMBOL", ); $mech->click ({ xpath => ('//input[@name="rbUploadType" and @value="li +st"]', single => 1), synchronize => 0 }); $mech->click({xpath => ('//*[@value="Submit List"]', single => 1), syn +chronize => 0}); $mech->back;

      In such cases, it is most helpfull to use the Firefox Document Inspector, which is invoked using F12 or CTRL+SHIFT+I. With it, you can point the mouse to a document element and it shows you the (generated) HTML source code that corresponds to that element.

      In the case of the sidebar, this would be the following HTML, which you can then manipulate using the usual WWW::Mechanize::Firefox methods:

      <select class="lists" style="z-index: -1;" name="speciesList" multiple +="" size="4"><option value="- Use All Species -"></option><option val +ue="0"></option><option value="1"></option><option value="2"></option +><option value="3"></option><option value="4"></option><option value= +"5"></option><option value="6"></option><option value="7"></option><o +ption value="8"></option><option value="9"></option><option value="10 +"></option><option value="11"></option><option value="12"></option><o +ption value="13"></option><option value="14"></option><option value=" +15"></option><option value="16"></option><option value="17"></option> +<option value="18"></option><option value="19"></option><option value +="20"></option><option value="21"></option><option value="22"></optio +n><option value="23"></option><option value="24"></option><option val +ue="25"></option><option value="26"></option><option value="27"></opt +ion><option value="28"></option><option value="-1"></option></select>

        Thanks again, Corion. For those interested, here's what worked for me. I included in my code sections for downloading a text file and closing windows which were subjects of the thread.
        If anyone has any advice on how to make this script run faster, I'd love to know how, but I'm just glad it works.

        (1) Install Firefox DOM Inspector add-on and open the DOM Inspector from Tools -> Web Developer -> DOM Inspector.
        (2) In the DOM Inspector, load the webpage from File -> Inspect Content Document.
        (3) Click "Inspect" at the top right to view the webpage, and select "Document-DOM Nodes" on the left and "Object-DOM Node" on the right, and click "Find a node to inspect by clicking on it" at the top left.
        (4) Click on the actual scrollbar to hightlight the whole listbox on the webpage (clicking the elements in the listbox didn't give useful information).
        (5) The information generated tells me that the listbox has the name 'speciesList', it's within the form 'frmManager', and the selection I want, "Saccharomyces cerevisiae(25)", has value '0'. I use this information with simple WWW::Mechanize::Firefox commands in my Perl script, as below.

        #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize::Firefox; use Firefox::Application; use WWW::Scripter; my $ff = Firefox::Application->new(); my $mech = WWW::Mechanize::Firefox->new(autoclose => 1); $mech->get("http://david.abcc.ncifcrf.gov/summary.jsp"); $mech->click({ xpath => ('//*[@href="Upload a Gene List or Population" +]', single => 1), synchronize => 0 }); $mech->form_name('frmManager'); $mech->set_fields( 'pasteBox' => "RRI1, YLR149C, FUN19, YBR285W, ALD3, CUE5, RTC3 +, AIM3, YDL199C, FMP45, AST2, GIP2, YKL091C, YKL133C, GLG2, SYM1, VHS3, YJR124C, MCH1, MHO1, I +KS1, RKM1, STF2, SAC1, ATG7, YAK1", 'Identifier' => "OFFICIAL_GENE_SYMBOL", ); $mech->click ({ xpath => ('//input[@name="rbUploadType" and @value="li +st"]', single => 1), synchronize => 0 }); $mech->click({xpath => ('//*[@value="Submit List"]', single => 1), syn +chronize => 0}); $mech->back; $mech->form_name('frmManager'); $mech->set_fields( 'speciesList' => 0); $mech->click({xpath => ('//*[@value="Select Species"]', single => 1), +synchronize => 0}); $mech->back; $mech->click({xpath => ('//*[@value="Functional Annotation Clustering" +]', single => 1), synchronize => 0}); my $retries = 100; while ($retries-- and ! $mech->is_visible( xpath => '//*[@value="Funct +ional Annotation Clustering"]' )) { sleep 1; $mech->back; } die "Timeout" unless $retries; my $dataurl; my @tab_info = $ff->openTabs(); foreach my $tab(@tab_info) { if ($tab->{location} =~ 'http://david.abcc.ncifcrf.gov/term2term.j +sp?') { $dataurl = $tab->{location}; chomp $dataurl; } } $mech->get($dataurl); my $w = new WWW::Scripter; my @foundLinks = $mech->find_all_links(); my $filelink; foreach my $link(@foundLinks) { if (index($link->[0], ".txt") > 0) { $filelink = $link->[0]; } } $w->get($filelink); if ($w->success()) { my $content = $w->response->content; open(FILE, ">DAVID.txt"); print FILE $content; close FILE; } my $var = `wmctrl -l | grep \"DAVID\" | tail -1 | cut -f1 -d\" \"`; chomp $var; system("wmctrl -i -c \"$var\"");

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-08-20 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (111 votes), past polls