简体   繁体   中英

ticking checkboxes using mechanize

I have the following html piece from this website: http://catalogue.bl.uk/F/NTBK6NCP3LK56BPNU2NJ4FF4MBLIRUNMFX66JLHKBDV1RNCAAB-02920?func=short-0&set_number=151530

<form name="XXX000001" method="GET"><input type="checkbox" name="ckbox"   onclick="javascript:select_record(this.form);" onkeypressed="javascript:select_record(this.form);"></form>

I was wondering if there is anyway I can use mechanize to tick the checkboxes? It seems that these boxes are not formed using the normal html. I don't know javascript

I assume you're talking about submitting information back to the page using Mechanize as if a human was using a browser to tick the checkboxes. The easiest way to do this is to first use WireShark to watch everything that happens when you actually submit the form with a web browser (with the checkboxes checked) and then mimic that same behavior when you submit the form using Perl.

Maybe you should check WWW::Selenium in order to deal with JavaScript web pages.

WWW::Selenium is a kind of remote control for browsers. From the documentation: Selenium Remote Control (SRC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. SRC provides a Selenium Server, which can automatically start/stop/control any supported browser. It works by using Selenium Core, a pure-HTML+JS library that performs automated tasks in JavaScript; the Selenium Server communicates directly with the browser using AJAX (XmlHttpRequest).

After you have downloaded and installed the Selenium server ( a Java apllication ), you can use the module WWW::Seletion , to control the server which will use browsers to interact with complex JavaScript enabled web sites.

With an instance of the class WWW::Selenium

my $sel = WWW::Selenium->new(
    host => "localhost",
    port => 4444,
    browser => "*iexplore",
    browser_url => "http://www.google.com",
);

almost every kind of interaction with a web site become possible. One has to define a locator to pinpoint an element in the page, in order to issue a command ( select, click, ... ).

You'll find detailed help in the module documentation.

由于WWW :: Mechanize不讲Java脚本,因此您要么按照建议执行操作,然后弄清楚JavaScript会做什么,然后绕开它的背面,否则您可以使用WWW :: Mechanize :: Firefox之类的控件来控制浏览器。

I have a suggestion that is admittedly not the most elegant solution but in the spirit of "gett'r done" you could try and create an autoit script that did some of the gui interactions for you. It is pretty neat little tool. I was bored of leveling my character in a game once so I created an autoit script to level my player for me and it only took me about a day to learn the syntax and create the script. I am sure automating a webpage would take much, much less time. Just a thought...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM