在IE浏览器中进行自动化,首先需要安装Win32::IEAutomation这个模块,此模块相当强大,能操作浏览器的刷新,后退,前进等,由于AutoItX3.dll也嵌在里面,甚至可以操作浏览器中弹出的windows窗口
在IEAutomation.pm的文件中,给出了使用它的例子:
1 use Win32::IEAutomation; 2 3 # Creating new instance of Internet Explorer 4 my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1); 5 6 # Site navigation 7 $ie->gotoURL('http://www.google.com'); 8 9 # Finding hyperlinks and clicking them 10 # Using 'linktext:' option (text of the link shown on web page) 11 $ie->getLink('linktext:', "About Google")->Click; 12 # Or using 'linktext:' option with pattern matching 13 $ie->getLink('linktext:', qr/About Google/)->Click; 14 # Or using 'id:' option ( <a id=1a class=q href=......>) 15 $ie->getLink('id:', "1a")->Click; 16 17 # Finding checkbox and selecting it 18 # Using 'name:' option ( <input type = "checkbox" name = "checkme" value = "1"> ) 19 $ie->getCheckbox('name:', "checkme")->Select; 20 # Or using 'aftertext:' option (for checkbox after some text on the web page) 21 $ie->getCheckbox('aftertext:', "some text here")->Select; 22 23 # Finding text field and entering data into it 24 # Using 'name:' option ( <input type="text" name="username" .......> ) 25 $ie->getTextBox('name:', "username")->SetValue($user); 26 27 # Finding button and clicking it 28 # using 'caption:' option 29 $ie->getButton('caption:', "Google Search")->Click; 30 31 # Accessing controls under frame 32 $ie->getFrame("name:", "content")->getLink("linktext:", "All Documents")->Click; 33 34 # Nested frames 35 $ie->getFrame("name:", "first_frame")->getFrame("name:", "nested_frame"); 36 37 # Catching the popup as new window and accessing controls in it 38 my $popup = $ie->getPopupWindow("title of popup window"); 39 $popup->getButton('value:', "button_value")->Click;
例子很详尽,只贴了一部分。对元素的操作放在Element.pm这个文件,在实际使用过程中有可能会发生找不到元素的情况,可通过更改它来实现。
如果要测360浏览器怎么办呢?简单,只要将默认浏览器改成360就可以啦。
控制台打印如果出现中文乱码的情况,可使用如下代码:
1 use encoding 'utf8',STDIN=>'utf8',STDOUT=>'gb2312';
运行起来的浏览器如果想输入中文,上面的就不能解决乱码了哦。需要对字符Encode即可,如下
$name = "测试"; $name2 = Encode::encode('gb2312', $name);