Summary of how to create a web browser automatic operation robot with Perl - Let's make a practical RPA with Perl (start writing)
Here is a summary of how to create a web browser automatic operation robot in Perl. For Windows users.
Install Strawberry Perl on Windows
To create a web browser auto-operation robot, you need Perl set up for Windows.
There is a choice between Active Perl and Strawberry Perl, but here we will use Strawberry Perl.
Strawberry Perl has a GUI operation module installed to create a web browser automatic operation robot. Mouse position specification, click, keyboard operation, clipboard operation, etc.
Perl module used to create a web browser automatic operation robot
Web browser automatic operation Perl modules used to create robots are mainly as follows.
- Win32::GuiTest - Mouse position specification, keyboard operation
- Win32::Clipboard - Clipboard operation
Window operation
On Windows, use Win32::GuiTest to get windows, position the mouse, click, and operate the keyboard. Loading the required function.
use Win32::GuiTest ( 'Unicode Semantics', 'GetDesktopWindow', 'GetParent', 'GetWindowText', 'GetClassName', 'GetChildWindows', 'GetChildDepth', 'MouseMoveAbsPix', 'SendLButtonUp', 'SendLButtonDown', 'Send Keys', );
Enable Unicode semantics
First of all, Perl is Unicode-enabled since Perl 5.8, so Win32::GuiTest will also be able to use Unicode-enabled semantics. It is assumed that the Perl source code is written in UTF-8 and "use utf8;" is written.
# Enable unicode semantics Unicode Semantics (1);
Get desktop window
To get the desktop window, use the GetDesktopWindow function. A desktop window is a window that shows the entire desktop.
# Get desktop window my $desktop_win = GetDesktopWindow ();
Get a list of child windows
In order to perform automatic operation of the web browser, it is necessary to get the window of the browser, but in order to do so, it is necessary to first get the list of child windows managed under the desktop window. Use GetChildWindows to get a list of child windows.
# Get a list of child windows my @windows = GetChildWindows ($desktop_win); # Loop child window for my $window (@windows) { }
Get the text of the window
Use the GetWindowText function to get the text of the window.
# Get the text of the window my $win_text = GetWindowText ($win);
The text in the window can vary depending on the browser type. In what I checked with Google Chrome, it was "Window title-Application name" as shown below.
Perl Club-Google Chrome
Get the target window
If you want to create a web browser automatic operation robot, you need to get the target window from the list of child windows. Write an example to get the target window. Make sure that the target page of the target web browser is launched.
# Get the target window (Perl Club-Google Chrome) my $win; { my $desktop_win = GetDesktopWindow (); die "Oops!" if GetParent ($desktop_win) != 0; my @browser_wins; my $browser_name = 'Chrome'; for my $win (GetChildWindows ($desktop_win)) { # Window name my $win_text = GetWindowText ($win); # Browser name if ($win_text =~ /\b $browser_name\b/) { if ($win_text =~ /Perl Club/) { push @browser_wins, $win; } } } my $browser_wins_count = @browser_wins; unless ($browser_wins_count == 1) { die "Must be open one window"); } $win = $browser_wins[0]; }Keyboard input # Go to Console tab SendKeys ('^]'); # Go to Elements tab SendKeys ('^['); # Move to the tag below SendKeys ('{DOWN}'); # Move to the tag above (body can be selected SendKeys ('{UP}');
SendKeys ('^c');
SendKeys ('^l'); SendKeys ('^v'); SendKeys ('{ENTER}'); SendKeys ('{TAB}' x $baibai_bukken_search_button_tab_press_count); SendKeys ('^v'); SendKeys ('{LEFT}'); SendKeys ('{RIGHT}' x $bukken_search_results_pagenation_forword_right_count_max); SendKeys ('{F12}'); SendKeys ('^]'); SendKeys ('^['); SendKeys ('{DOWN}'); SendKeys ('{UP}'); SendKeys ('^c'); SendKeys ('{F12}');->
Clipboard
Use the Win32::Clipboard module to work with the Windows clipboard.
use Win32::Clipboard;
Clipboard object generation
First create a clipboard object.
# Clipboard object creation my $clip = Win32::Clipboard->new;
Save Perl string to clipboard
Save the Perl string with the Set method. It is assumed that the Perl source code is written in UTF-8 and "use utf8;" is written. When saving a string from the Perl side to the Windows clipboard, encode it in cp932 and pass it. This is because the character code of the text on the Windows clipboard is cp932.
# Save string to clipboard $clip->Set(encode('cp932', $string));
Extract Perl string from clipboard
By specifying "CF_UNICODETEXT" in the argument of the GetAs method, it can be obtained in one Unicode encoding format called "UTF16-LE". Convert this to a Perl string using the Perl Encode function decode function. You can get the contents of the Windows clipboard as a Perl string. Please note that you cannot get it correctly with just the GetText function.
# Fetch a Perl string from the clipboard my $string = $CLIP->GetAs(CF_UNICODETEXT); $text = Encode::decode("UTF16-LE", $string);
Paste the contents of the clipboard to the form part
Send "Ctrl + c" to paste the contents of the clipboard into a text field or form part such as a text area. "^" Means "Ctrl".
# Paste the contents of the clipboard into a form part SendKeys ('^c');
Points for creating RPA robots
Wait for browser response
If you want to wait for the browser response, use the sleep function. Set the seconds as safe as possible when the browser response is returned. However, it depends on the network conditions, so it may happen that the next operation starts and the operation changes without returning.
# Wait 3 seconds sleep 3;
Notes on RPA robots
RPA is a method with many problems in processing automation and operational efficiency. RPA robots are impressed with their movement the moment they see them, but keep in mind that processing speed, program maintainability, and automation are unreliable.
"What if the GUI screen changes due to a Windows upgrade?" "What if the button position or text display changes due to an update of the Web service?" "What if the Web response does not come back for 5 seconds?" " Who starts the program every day? What if I forget it? "" How do you work with other programs running on Linux? "
The first basic and effective methods that can be taken to automate processing and improve work efficiency are text conversion, command conversion, Web conversion, batching, and RDBMS conversion.
Why not consider these five basic measures first? RPA will be effective for a limited area.
(Still continuing)