Often we need to read data from a file and setting of the cycle in a field. Write a simple script that will say read from the file search requests entered into Google search box and click on the search button.
First, we'll create a script using the File menu, click New Menu. In the editor php-stream program will open a new tab with the following code:
? php
// The following code is required to properly run XWeb Human Emulator
require ("../Templates/xweb_human_emulator.php ");
// Navigate to google
$browser->navigate("http://www.google.com");
// Wait on browser
$browser->wait(1);
// Quit
$app->quit();
?>
If you run this script, then the browser program will open Google.
Now the mouse cursor is over in the search box, we click the right mouse button and see the popup menu.
One of the items is $input leads to another menu, it separates the functions for the object $input. Click on the $input->set_value_by_name("q ",""); menu and this string appears in the php editor. Do the same with the "Search Google" button. Context menu $button and drop-down menu choose $button->click_by_name("btnG");. This line is added to the editor in the script.
Note: before inserting thus function, place the cursor at the location where you want the script to insert the otherwise it will be inserted where there is currently the cursor!
Immediately after the function $button->click_by_name("btnG"); add $browser->wait(1);. This function is mandatory after every function of transition or upgrade your browser. She expects until the browser will perform specified actions, and then executes the script further.
Now the script looks as follows:
? php
// The following code is required to properly run XWeb Human Emulator
require ("../ Templates / xweb_human_emulator.php ");
// Navigate to google
$browser->navigate("http://www.google.com");
// Wait on browser
$browser-> wait (1);
$input->set_value_by_name ( "q ","");
$button->click_by_name ( "btnG");
$browser->wait (1);
// Quit
$app->quit();
?>
When executed, this script is an empty string is entered in the search box and clicks the Search button.
Now connect queries from a file and will introduce them in the search box in the cycle.
For this first prochiatem file into an array with the following code:
$arr_search = file("c:\\search_string.txt")
If the file is asking for is in the same folder as the script then absolutely path may be omitted and the code is as follows:
$arr_search = file("search_string.txt")
Function PHP file reads each line of the file in a separate element of the array.
Now organize a cycle based on the number of elements in the array, ie kolichesvto lines read from the file:
loop - for($i = 0; $i < ($arr_search); $i++)
loop body - ()
that would get the current element masciva must add this code that's $arr_search[$ i]
eventually have the following code:
? php
// The following code is required to properly run XWeb Human Emulator
require ("../ Templates / xweb_human_emulator.php ");
// Read data from a file into an array
$arr_search = file ( "search_string.txt")
// Organize the script
for ($i = 0; $i < ($arr_search); $i++)
{
// Navigate to google
$browser->navigate("http://www.google.com");
// Wait on browser
$browser->wait(1);
// Print request to the debug window
echo $arr_search[$i]. "br>";
// Enters a query into the search box
$input->set_value_by_name("q", $ arr_search [$ i]);
// Push the search button
$button->click_by_name ( "btnG");
$browser->wait(1);
// Look at the results of 5 seconds:)
sleep (5);
// ... here you can continue to do anything with the results of search to gather them in a file
// Go to each site and parse the text in the file, etc.
}
// Quit
$app->quit();
?>
When you run the script to execute script reads all rows from the file search_string.txt set into the search field and clicks on the Search button, after waits 5 seconds and moves on.
No comments:
Post a Comment