Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by jbernest (Novice)
on Apr 28, 2013 at 20:08 UTC ( #1031110=perlquestion: print w/ replies, xml ) Need Help??
jbernest has asked for the wisdom of the Perl Monks concerning the following question:

I'm using WWW::Mechanize::Firefox to script a website. The second URL opens in a new browser window. How can I close this new browser window in my script? If anyone tries this code, you need to have the "MozRepl 1.1" add-on installed and started in Firefox. Thanks in advance.

#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $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; my $retries = 100; while ($retries-- and ! $mech->is_visible( xpath => '//*[@value="Funct +ional Annotation Clustering"]' )) { sleep 1; }; die "Timeout" unless $retries; $mech->click({xpath => ('//*[@value="Functional Annotation Clustering" +]', single => 1), synchronize => 0}); $mech->get("http://david.abcc.ncifcrf.gov/term2term.jsp?annot=52,9,79, +85,25,32,39,3,47,45,63,76&currentList=1");

Comment on WWW::Mechanize::Firefox -- how to close firefox browser window
Download Code
Re: WWW::Mechanize::Firefox -- how to close firefox browser window
by jbernest (Novice) on Apr 28, 2013 at 23:42 UTC

    I figured out a solution. I installed the "WMCTRL" package, a linux windows manager, using "sudo apt-get install wmctrl". Now I use the "wmctrl -c "title of window"" system command in my perl script to close only the browser window with that title. See the 2 new lines of code below.

    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $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; my $retries = 100; while ($retries-- and ! $mech->is_visible( xpath => '//*[@value="Funct +ional Annotation Clustering"]' )) { sleep 1; }; die "Timeout" unless $retries; $mech->click({xpath => ('//*[@value="Functional Annotation Clustering" +]', single => 1), synchronize => 0}); $mech->get("http://david.abcc.ncifcrf.gov/term2term.jsp?annot=52,9,79, +85,25,32,39,3,47,45,63,76&currentList=1"); my $title = 'DAVID: Database for Annotation, Visualization, and Integr +ated Discovery (Laboratory of Immunopathogenesis and Bioinformatics ( +LIB); National Institute of Allergies and Infectious Diseases (NIAID) +; Science Applications International Corporation (SAIC)'; system("wmctrl -c \"$title\"");
Re: WWW::Mechanize::Firefox -- how to close firefox browser window
by jbernest (Novice) on Apr 29, 2013 at 03:37 UTC

    Here's a more robust solution. It's possible to have multiple web browser windows with the same title. In that case, I wouldn't want to close all windows with a given title, just the last window that opened. So, the last three lines in this script identify the numeric value associated with the last window that opened containing "DAVID" in the title, then uses the system command to close that window.

    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $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; my $retries = 100; while ($retries-- and ! $mech->is_visible( xpath => '//*[@value="Funct +ional Annotation Clustering"]' )) { sleep 1; }; die "Timeout" unless $retries; $mech->click({xpath => ('//*[@value="Functional Annotation Clustering" +]', single => 1), synchronize => 0}); $mech->get("http://david.abcc.ncifcrf.gov/term2term.jsp?annot=52,9,79, +85,25,32,39,3,47,45,63,76&currentList=1"); my $var = `wmctrl -l | grep \"DAVID\" | tail -1 | cut -f1 -d\" \"`; chomp $var; system("wmctrl -i -c \"$var\"");
Re: WWW::Mechanize::Firefox -- how to close firefox browser window
by Corion (Pope) on Apr 29, 2013 at 07:23 UTC

    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

      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>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-09-18 01:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (104 votes), past polls