fsquirrelgpr Posted August 8, 2005 Posted August 8, 2005 I saw the posting with some code for navigating in Internet Explorer so thought I would post the functions I have been using for navigating in Firefox (entering information, navigating tables, activating windows, etc). These were cleaned up as not to be specific to the web-pages I use them with, but I might have missed a couple of references or broken a couple of functions by renaming variables and eliminating window names/usernames/passwords. I am sure there are better ways of doing many of these tasks but thought this might be useful for some, also remember I was (and still am) learning the language as I wrote these functions: expandcollapse popup#include-once ;This is an include file ; ; AutoIt Version: 3.0 ; Language: English ; Platform: Win9x/NT (Mozilla Firefox) (Windows Server 2003) ; Author: Gregory Roby ; It assumes that Mozilla Firefox will be used (or Deer Park Alpha 2) ; Created originally 4/20/2005 by Gregory Roby, last modified 8/8/2005 ;Put the following line in any program that needs access to this file (assuming it has not moved) #include <file.au3> Global $standard_delay = 40 Global $short_delay = 1000 Global $medium_delay = 6000 Global $long_delay = 9000 global $win_activate_timeout = 75000; 75 seconds to activate a window global $current_position[2] global $run_function_on_exit_program = "none" ;$purple_button is the color of Buttons (dark purple) It varies a bit on different computers, use spread to find it global $purple_button = 132 global $purple_button_spread = 25 ;$firefox_green is the color of highlighted text when searched for using firefox global $firefox_green = 3725432 global $firefox_green_spread = 25 ; $blue_link is the blue color when a link is highlighted global $blue_link = 255 global $blue_link_spread = 15 ; $Deer_Park_Green is the green color of a the active link in Deer Park Alpha 2 (at least on my computer, not even sure what setting I changed to get it) global $deer_park_green = 1144610 global $deer_park_green_spread = 5 ;These are the names of Windows Global $win_mozilla = "Mozilla Firefox" Global $win_security = "Security Error" global $win_report = "IIPdfReport" global $win_print = "print" global $win_command = "c:\windows\system32\command.com" global $win_deer = "deer" Global $progress_file_path = "p:\networ~1\progress\" global $progress_file_name = "default.txt" global $mozilla_directory = "c:\program files\mozilla firefox\" ;$pixels_to_check is the number of pixels to scan on a line after moving right to reposition the mouse cursor to the ;same position, $pixel_row is the array that stores the color values of the pixels global $pixels_to_check = 225 global $pixel_row[1000] ;Color of Button When Mouse Overed (with our billing system) global $blue_button_mouseover = 3355545 global $blue_button_mouseover_spread = 25 ;these variables is set by calling max_coords() global $max_x global $max_y ;is the color of the dividing lines for the tables in Exception Manager global $grey_table = 9276813 global $grey_table_spread = 25 ; assumes the window needing the info entered is active, ;It attempts to search for the pixel color of the search string highlighed using Mozilla ;and enters the_info in the very next field func enter_info($the_info, $the_search_string, $optional_delay = 40) opt("sendkeydelay", $optional_delay) local $the_position[2] send("^f") send($the_search_string) $the_position = pixelsearch(0, 25,1280,1024,$firefox_green, $firefox_green_spread,3) mousemove($the_position[0], $the_position[1]) sleep($short_delay / 4) mouseclick("left") sleep($short_delay / 4) send("{tab}") opt("sendkeydelay", $optional_delay / 2) send ($the_info) opt("sendkeydelay", $standard_delay) EndFunc func open_command() run("c:\windows\system32\command.com", "") my_activate_window($win_command, "Could not open a command prompt.") EndFunc func run_command($the_command) Opt("SendKeyDelay", 10) send($the_command, 1) send("{enter}") sleep($short_delay / 4) Opt("SendKeyDelay", $standard_delay) EndFunc func close_command() send("exit{enter}") wait_for_window_to_close($win_command) EndFunc ;*************************************************************************************************** ********************* ;***********The following functions deal with navigating through tables (including scrolling right)*********** ;*************************************************************************************************** ********************* func next_box_right($boxes_right = 1) local $boxes_moved = 0 for $boxes_moved = 1 to $boxes_right local $reference_x = 0 local $had_to_move_right = 0 Do ;Always want to have a box to the right of the current box ;so First, go right until a line is found (will always happen unless there is something unaccounted for wrong) $current_position[0] = $current_position[0] + 1 mousemove($current_position[0], $current_position[1], 0) if($current_position[0] = $max_x) Then exit_program("When searching for the next_box_right, could not find the right hand side of the current box.") EndIf until(pixelgetcolor($current_position[0], $current_position[1]) = $grey_table) $reference_x = $current_position[0] ;Now you are at the next box to the right, but not sure if the right side of that box is on the screen yet, so ;do these steps to make sure: local $loop_problem = 0 $current_position[0] = $current_position[0] + 2 while(another_box_to_the_right($current_position[0], $current_position[1]) = 0) ;being here in the loop statement means there is not a right edge to the current box on the screen ;move mouse pointer left to the last grey line Do $current_position[0] = $current_position[0] - 1 until(pixelgetcolor($current_position[0], $current_position[1]) = $grey_table) ;now get some pixels to the left of this line so we can find where we were get_pixels_to_left($current_position[0], $current_position[1], $pixels_to_check) ;get_pixels_to_left are stored in the global array $pixel_row ;now actually move over to the right (hit right arrow key a few times) move_right() $loop_problem = $loop_problem + 1 if($loop_problem = 50) Then exit_program("Got stuck in the loop trying to find another box to the right (could not find one after 50 moves to the right).") EndIf sleep($short_delay) ;now set the x coordinate to be where it was before we cold not find another_box_to_the_right $current_position[0] = find_end_of_pixels($pixels_to_check) WEnd ;then move off the dividing line $current_position[0] = $current_position[0] + 3 Next EndFunc func find_end_of_pixels($number_of_pixels) local $temp_x = 0 local $current_x = 0 local $found_match = 1 Do $found_match = 1 for $temp_x = $current_x to $current_x + $pixels_to_check if not (pixelgetcolor($temp_x, $current_position[1]) = $pixel_row[$temp_x - $current_x]) Then $temp_x = $current_x + $pixels_to_check $found_match = 0 EndIf $temp_x = $temp_x + 1 Next $current_x = $current_x + 1 if($current_x = $max_x) Then exit_program("Could not find the pixel row after scrolling to the right") EndIf until $found_match = 1 $current_x = $temp_x + 4 return $current_x EndFunc func another_box_to_the_right($x_start, $y_position) Do $x_start = $x_start + 1 if($x_start = $max_x) Then return 0 EndIf until(pixelgetcolor($x_start, $y_position) = $grey_table) return 1 EndFunc func get_pixels_to_left($x_start, $y_position, $number_of_pixels) local $i local $temp_x = $x_start $temp_x = $x_start - $number_of_pixels for $i = 0 to $number_of_pixels - 1 $pixel_row[$i] = pixelgetcolor($temp_x, $y_position) $temp_x = $temp_x + 1 Next EndFunc func move_right() send("{right}") sleep($short_delay) EndFunc ;This function copies the contents of the current box. It also moves the mouse position to the beginning of the next box. ;Contents will be on the clipboard func copy_current_box() next_box_right() local $copy_temp_x = $current_position[0] Do $copy_temp_x = $copy_temp_x - 1 until(pixelgetcolor($copy_temp_x, $current_position[1]) = $grey_table) $copy_temp_x = $copy_temp_x - 3 mousemove($copy_temp_x, $current_position[1], 0) sleep($short_delay / 2) mousedown("left") Do $copy_temp_x = $copy_temp_x - 1 Until(pixelgetcolor($copy_temp_x, $current_position[1]) = $grey_table) $copy_temp_x = $copy_temp_x - 3 mousemove($copy_temp_x, $current_position[1], 0) sleep($short_delay / 2) mouseup("left") sleep($short_delay / 2) send("^c") sleep($short_delay / 2) mousemove($current_position[0], $current_position[1], 0) EndFunc ;*************************************************************************************************** ********************* ;***********The preceding functions deal with navigating through tables (including scrolling right)*********** ;*************************************************************************************************** ********************* ;This function waits until the hourglass goes away func wait_until_done($max_wait = 120000) local $counter = 0 while(mousegetcursor() = 1) $counter = $counter + $short_delay sleep($short_delay) if($counter > $max_wait) Then exit_program("Stuck in wait until done loop, exited after " & string($counter) & "ms, mousegetcursor is: " & string(mousegetcursor())) EndIf WEnd sleep($short_delay / 4) endfunc ;This function will close all opened mozilla windows (used by this include file) func cleanup() winclose($win_command) winclose($win_deer) EndFunc ; This function attempts to activate a window. It will exit the script if the window cannot be activated. ; It keeps trying until $win_activate_timeout is reached func my_activate_window($win_name, $fail_reason, $maximize = "yes") local $seconds = 0 ;try to activate the window for up to $win_activate_timeout then fail Do winactivate($win_name) if(winactive($win_name)) then $seconds = $win_activate_timeout if($maximize = "yes") Then winsetstate($win_name, "", @SW_MAXIMIZE) EndIf EndIf sleep($short_delay / 2) $seconds = $seconds + ($short_delay / 2) until $seconds > $win_activate_timeout if(not winactive($win_name)) then exit_program($fail_reason) EndIf wait_until_done() EndFunc func max_coords() local $temp_pos[2] local $max_pos[2] $temp_pos = mousegetpos() mousemove(5000, 5000, 0) $max_pos = mousegetpos() mousemove($temp_pos[0], $temp_pos[1], 0) $max_x = $max_pos[0] $max_y = $max_pos[1] EndFunc func wait_for_window_to_close($window_name) winactivate($window_name) Do sleep($short_delay) until(not winactive($window_name)) EndFunc func reset_current_position() $current_position[0] = 0 $current_position[1] = 0 endfunc ;Exit program uses a string variable ;It gives a slightly more user friendly exit then otherwise might occur. ;The goal is to exit if it is detected that a script is not working rather then have it do random stuff on the ;screen for extended periods of time ;It also checks the global variable $run_function_on_exit_program, if it is set it will call the selected function instead of exiting ;This is used for a script to continue working even when an error is encountered (set to none by default) func exit_program($exit_reason) if($run_function_on_exit_program = "none") Then cleanup() if(not ($exit_reason = "exit")) Then $exit_reason = "The problem was related to: " & $exit_reason msgbox(1,"Script Aborted", $exit_reason) exit 0 EndIf EndIf call($run_function_on_exit_program) exit 0 EndFunc ;between_commas will return the the information between which_comma and which_comma + 1 ;for example, if which_comma = 0, it will return everything before the first comma ;it needs a string and which comma (integer) as arguments func between_commas($the_string, $which_comma) Local $current_comma = 0 Local $left_bound = 0 Local $temp_string local $right_bound = stringlen($the_string) local $character_position local $separator = "," local $quote_character = '"' local $in_quotes = 0 ;This section finds the left bounds that we actually want $character_position = 0 while($current_comma < $which_comma) if(stringmid($the_string, $character_position, 1) = $quote_character) then if($in_quotes = 0) Then $in_quotes = 1 Else $in_quotes = 0 EndIf EndIf if($in_quotes = 0) Then if(stringmid($the_string,$character_position,1) = $separator) then $current_comma = $current_comma + 1 EndIf EndIf $character_position = $character_position + 1 if($character_position > $right_bound) Then exit_program("function betwen_commas is trying to find comma: " & $which_comma & " but only found: " & $current_comma & " commas. Exceeded length of array which was " & $right_bound & "The string is " & $the_string) EndIf WEnd $temp_string = "" $in_quotes = 0 while($character_position < $right_bound + 1) if(stringmid($the_string, $character_position, 1) = $quote_character) Then $character_position = $character_position + 1 if($in_quotes = 0) Then $in_quotes = 1 Else $in_quotes = 0 EndIf EndIf if($in_quotes = 0) Then if(stringmid($the_string, $character_position, 1) = $separator) Then $character_position = $right_bound + 1 Else $temp_string = $temp_string & stringmid($the_string, $character_position, 1) $character_position = $character_position + 1 EndIf Else $temp_string = $temp_string & stringmid($the_string, $character_position, 1) $character_position = $character_position + 1 EndIf WEnd return $temp_string EndFunc ;This function will convert any "year" numbers for 1990-2090 to a 2 digit year by stripping off the left two characters ;it uses a brute force approach func convert_date($the_date) local $date_conversion_counter local $temp_date = $the_date for $date_conversion_counter = 1990 to 2090 $temp_date = stringreplace(stringstripws(" " & $temp_date,8), stringstripws(" " & $date_conversion_counter, 8), stringright($date_conversion_counter, 2)) Next return $temp_date EndFunc ;This function makes sure that the specified folder exists, if not, it creates it ;only looks one folder deep so check each level func make_sure_exists($specified_folder) if(not fileexists($specified_folder)) Then open_command() run_command('md "' & $specified_folder & '"') close_command() EndIf EndFunc ;click on Found assumes the window needing the info entered is active, ;It attempts to search for the pixed color of the search string highlighed using Mozilla ;and clicks on whatever is highlighted ;flag is what flag (if any) to use for sending the search string func click_on_found($the_search_string, $x_offset = 0, $y_offset = 0, $flag = 0) opt("sendkeydelay", $standard_delay) local $the_position[2] send("^f") send($the_search_string, $flag) $the_position = pixelsearch(0, 25,1280,1024,$firefox_green, $firefox_green_spread,3) mousemove($the_position[0] + $x_offset, $the_position[1] + $y_offset) sleep($short_delay / 2) mouseclick("left") wait_until_done() sleep($short_delay / 2) EndFunc ;This function replaces the special "send" characters in a string with those characters in brackets ;and returns the result func string_simple($input_string) local $the_string = $input_string $the_string = stringreplace($the_string, "{", "{{}") $the_string = stringreplace($the_string, "}", "{}}") $the_string = stringreplace($the_string, "+", "{+}") $the_string = stringreplace($the_string, "^", "{^}") $the_string = stringreplace($the_String, "!", "{!}") $the_string = stringreplace($the_string, "#", "{#}") return $the_string EndFunc ;This function sets alternate window names for Deer Park (Mozilla Alpha) func alternate_deer_windows() Opt("SendKeyDelay", $standard_delay) ;5 milliseconds Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=... $win_mozilla = "Deer Park" $mozilla_directory = "c:\program files\Deer Park Alpha 2\" EndFunc ;This function does only some of the cleanup tasks so that windows which can be reused are reused to speed things up a bit func partial_cleanup() winclose($win_command) EndFunc ;This function adds a tick to the progress file (number 1) so its size can be checked to make sure progress is continuing without ;have to make complicated functions to see what a script is actually doing func add_tick_to_progress_file() $progress_file = fileopen($progress_file_path & $progress_file_name, 1) filewrite($progress_file, "1") fileclose($progress_file) EndFunc func get_progress_file_size() return(filegetsize($progress_file_path & $progress_file_name)) EndFunc ;This function removes the first entry from a file and puts it into the 2nd file func remove_first_entry($file_one_name, $file_two_name) local $single_line = "" local $counter local $error_file_as_array[25000] local $the_original_file _filereadtoarray($file_one_name, $error_file_as_array) ;This section writes everything back to the error file except for the 1st line sleep($short_delay / 2) $the_original_file = fileopen($file_one_name, 2) for $counter = 2 to $error_file_as_array[0] if(stringlen($error_file_as_array[$counter]) > 5) Then $error_file_as_array[$counter] = stringstripcr($error_file_as_array[$counter]) $error_file_as_array[$counter] = stringreplace($error_file_as_array[$counter], @LF, "") FileWriteline($the_original_file, $error_file_as_array[$counter]) EndIf Next fileclose($the_original_file) ;This section appends the log file with the first line from the other file (gets name from global variable $current_error_file) local $log_file $log_file = fileopen($file_two_name, 1) if(stringlen($error_file_as_array[1]) > 5) then $error_file_as_array[1] = stringstripcr($error_file_as_array[1]) $error_file_as_array[1] = stringreplace($error_file_as_array[1], @LF, "") FileWriteline($log_file, $error_file_as_array[1]) EndIf fileclose($log_file) EndFunc ;This function finds the left and right side of the $text_to_find ;and sets $left and $right = to them ($right is optional) func location_of_text_horizontal($text_to_find, $left, $right = 0) send("^f") send($text_to_find) local $temp_position[2] $temp_position = pixelsearch(0, 25,1600,1200,$firefox_green, $firefox_green_spread,3) $left = $temp_position[0] $right = $left + 250 Do $right[0] = $right[0] - 1 Until (pixelgetcolor($right, $temp_position[1]) = pixelgetcolor($left, $temp_position[1])) EndFunc ;This function finds the top and bottom of selected text and sets top and bottom to that location func location_of_text_vertical($text_to_find, $top, $bottom = 0) send("^f") send($text_to_find) local $temp_position[2] $temp_position = pixelsearch(0, 25,1600,1200,$firefox_green, $firefox_green_spread,3) $top = $temp_position[1] $bottom = $top + 250 Do $bottom = $bottom - 1 Until (pixelgetcolor($temp_position[0], $bottom) = pixelgetcolor($temp_position[0], $top)) EndFunc ;This function removes both @LF's and @CR's from a string, returning the result func no_lf_or_cr($the_string) local $new_string $new_string = stringstripcr($the_string) $new_string = stringreplace($new_string, @LF, "") return $new_string EndFunc
dinzing Posted April 27, 2008 Posted April 27, 2008 Sweet just what iv been looking for TY greatly for posting it now i just have to rescript ever thing to the fox. oh wells should be fun and gives me more reason to use/learn AutoIT faster ;-) "Time Is God"
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now