Sign in to follow this  
Followers 0
JFee

Read text from a firefox window?

11 posts in this topic

Is there any way to read text from a FireFox window? WinGetText returns "".

I have a page the is constantly reloading, and I want to check for a certain text, and if it exists I am going to take a screenshot.

Thanks


Regards,Josh

Share this post


Link to post
Share on other sites



This is a topic that is oft repeated.

The answer is no.

The full answer is found by using the search function.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Try this.

See the comments throughout the code.

Dave

CODE

#comments-start

This script periodically checks a Firefox web page for a certain word or phrase.

When the text is found, a message box is displayed.

The script checks the background color of Firefox's "Find" box in the lower left corner,

so ensure that it is not covered by the task bar or by another window.

Set the coordinates to check for the color based on whether Firefox's status bar is on or not.

Defaults for this script are a resolution of 1280 x 1024 with Firefox's status bar ON.

Press the "`" key to exit the script.

On many keyboards, that's the key to the left of the number 1 key

in the upper left corner of the keyboard.

#comments-end

; Press the "`" key to exit the script.

HotKeySet("`", "End")

; Enter text to watch for.

$lookfor = InputBox("Enter search text", "Enter search text")

$rate = InputBox("Check how often?", "Enter rate in milliseconds at which to check. Entering 2000 will check every two seconds.")

; Note: This script does not check if $rate is a valid number.

; Select window to watch.

; 262144 = Message box stays on top so user can click the OK button

; after clicking window to watch.

MsgBox(262144, "Select window to watch", "Click on the window to watch for " & $lookfor & ", then click OK. Press the ` key to exit the script.")

; Get the title so we can activate this window later.

$lookhere = WinGetTitle("")

WinActivate($lookhere)

; Start endless loop.

While 1

; Optional: Use WinActivate to switch to $lookhere window

; in case another window has become active since the last search.

; WinActivate($lookhere)

; Open Find dialog and enter $lookfor text.

Send("^f")

Sleep(100)

Send($lookfor)

; Put x-position, y- position, width, height of current window in $size[] array.

$size = WinGetPos("")

#comments-start

$size[0] = X Position of window (Pixels from left edge of wallpaper to left edge of window)

$size[1] = Y Position (Pixels from top edge of wallpaper to top of window)

$size[2] = Width of window

$size[3] = Height of window

Check color of pixel at coordinates X,Y where

X = Horizontal window position + # of pixels from left edge of window to background of Find box, and

Y = Vertical window position + height - # of pixels from bottom edge of window to background of Find box.

Tested screen resolution was 1280 x 1024.

If your resolution is different, you might need to change "75" and "33"

in the following MouseMove and PixelGetColor commands.

Make sure that the pixel you check is in a corner of the Find box,

away from the text in the box. If the $lookfor text is not found,

the background of the Find box will be red, and the text in the box will be white.

You want to check the Find box's background, not the text in the box.

Use MouseMove to position the mouse cursor on a pixel to check for color.

This mouse move is not required for checking the Find box's background color.

It just helps the user to see which pixel is being checked.

#comments-end

; Uncomment the next line to move the mouse cursor to the pixel being checked for color.

; MouseMove($size[0] + 75, $size[1] + $size[3] - 33, 0)

; Get color of search box background. ( white = 16777215)

$var = PixelGetColor($size[0] + 75, $size[1] + $size[3] - 33)

Sleep(100)

; Close Find dialog

Send("{ESC}")

If $var = 16777215 Then ;

; Background is white, which means text is found.

; Use AutoIt's SoundPlay command to play a sound here if you want.

MsgBox(0, $lookfor & " found!", "Replace this message box with screen capture!")

; Save screen capture now.

Else

; Background is not white if text is not found.

; Keep waiting and watching for it.

EndIf

; ============================================

; Pause for $rate milliseconds.

Sleep($rate)

WEnd

Func End()

MsgBox(0, "Script ended", "` key pressed. Script ended")

Exit

EndFunc ;==>End

Edited by davelf

Share this post


Link to post
Share on other sites

ff.au3 will give you the text of the current page, without tags.

You will still have to write a script to search that text for your desired text.

You could copy a page of the text to Notepad and search it there.

Select all text in Firefox with CTRL-a.

Copy text to clipboard.

Paste to Notepad.

Search for desired text in Notepad.

If text is not found, Window title changes from "Find" to "Notepad."

If text is found, the "Find" dialog stays active.

Dave

Share this post


Link to post
Share on other sites

ff.au3 will give you the text of the current page, without tags.

You will still have to write a script to search that text for your desired text.

You could copy a page of the text to Notepad and search it there.

Select all text in Firefox with CTRL-a.

Copy text to clipboard.

Paste to Notepad.

Search for desired text in Notepad.

If text is not found, Window title changes from "Find" to "Notepad."

If text is found, the "Find" dialog stays active.

Dave

Last time I checked that was called StringInStr()

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Last time I checked that was called StringInStr()

Thanks for the tip, Distrophy.

The following code copies text to the clipboard using plain old CTRL-A and CTRL-C, then uses StringInStr() to search the contents of the clipboard.

Dave

CODE
#comments-start

This script periodically checks a Firefox web page for a certain word or phrase.

When the text is found, the word or phrase is highlighted, and the browser window

is copied to the clipboard.

Press the "`" key to exit the script.

On many keyboards, that's the key to the left of the number 1 key

in the upper left corner of the keyboard.

#comments-end

; Press the "`" key to exit the script.

HotKeySet("`", "End")

; Enter text to watch for.

$lookfor = InputBox("Enter search text", "Enter search text")

; Exit if cancel button is pushed.

If @error = 1 Then Exit

$rate = InputBox("Check how often?", "Enter rate in milliseconds at which to check. Entering 2000 will check every two seconds.", "2000")

; Note: This script does not check if $rate is a valid number.

; Exit if cancel button is pushed.

If @error = 1 Then Exit

; Select window to watch.

; 262144 + 1 = Message box stays on top so user can click the OK or cancel button.

; after clicking window to watch.

$which_window = MsgBox(262145, "Select window to watch", "Click on the window to watch for " & $lookfor & ", then click OK. Press the ` key to exit the script.")

; Exit if cancel button is pushed.

If $which_window = 2 Then Exit

; Get the title so we can activate this window later.

$lookhere = WinGetTitle("")

WinActivate($lookhere)

; Watch for desired text.

Do

; Pause for $rate milliseconds.

Sleep($rate)

; Optional: Use WinActivate to switch to $lookhere window

; in case another window has become active since the last search.

; WinActivate($lookhere)

; Select All and copy to clipboard

Send("^a^c")

; Deselect all.

Send("{LEFT}")

; Put text from clipboard into a string.

; Images are ignored.

; This will copy at least the first 1on the page.

; Note: Script tested OK with 15,000 characters.

; There's probably a limit to how many characters you can copy.

$text = ClipGet()

Until StringInStr($text, $lookfor) > 1 ; Until desired text is found.

MsgBox(4096, "'" & $lookfor & "' found.", "'" & $lookfor & "' found.")

WinActivate($lookhere) ; In case window has lost focus.

; Go To top of page

Send("^{HOME}")

; Highlight desired text by searching for it using Find.

Send("^f")

Sleep(100)

Send($lookfor)

; Close Find dialog

Send("{ESC}")

;Copy screen contents to clipboard

Send("!{PRINTSCREEN}")

MsgBox(4096, "Screen capture status", "Desired text is highlighted. Image of browser window has been copied to the clipboard.")

Func End()

MsgBox(0, "Script ended", "` key pressed. Script ended")

Exit

EndFunc ;==>End

Edited by davelf
1 person likes this

Share this post


Link to post
Share on other sites

Thanks for the tip, Distrophy.

The following code copies text to the clipboard using plain old CTRL-A and CTRL-C, then uses StringInStr() to search the contents of the clipboard.

Dave

CODE
#comments-start

This script periodically checks a Firefox web page for a certain word or phrase.

When the text is found, the word or phrase is highlighted, and the browser window

is copied to the clipboard.

Press the "`" key to exit the script.

On many keyboards, that's the key to the left of the number 1 key

in the upper left corner of the keyboard.

#comments-end

; Press the "`" key to exit the script.

HotKeySet("`", "End")

; Enter text to watch for.

$lookfor = InputBox("Enter search text", "Enter search text")

; Exit if cancel button is pushed.

If @error = 1 Then Exit

$rate = InputBox("Check how often?", "Enter rate in milliseconds at which to check. Entering 2000 will check every two seconds.", "2000")

; Note: This script does not check if $rate is a valid number.

; Exit if cancel button is pushed.

If @error = 1 Then Exit

; Select window to watch.

; 262144 + 1 = Message box stays on top so user can click the OK or cancel button.

; after clicking window to watch.

$which_window = MsgBox(262145, "Select window to watch", "Click on the window to watch for " & $lookfor & ", then click OK. Press the ` key to exit the script.")

; Exit if cancel button is pushed.

If $which_window = 2 Then Exit

; Get the title so we can activate this window later.

$lookhere = WinGetTitle("")

WinActivate($lookhere)

; Watch for desired text.

Do

; Pause for $rate milliseconds.

Sleep($rate)

; Optional: Use WinActivate to switch to $lookhere window

; in case another window has become active since the last search.

; WinActivate($lookhere)

; Select All and copy to clipboard

Send("^a^c")

; Deselect all.

Send("{LEFT}")

; Put text from clipboard into a string.

; Images are ignored.

; This will copy at least the first 1on the page.

; Note: Script tested OK with 15,000 characters.

; There's probably a limit to how many characters you can copy.

$text = ClipGet()

Until StringInStr($text, $lookfor) > 1 ; Until desired text is found.

MsgBox(4096, "'" & $lookfor & "' found.", "'" & $lookfor & "' found.")

WinActivate($lookhere) ; In case window has lost focus.

; Go To top of page

Send("^{HOME}")

; Highlight desired text by searching for it using Find.

Send("^f")

Sleep(100)

Send($lookfor)

; Close Find dialog

Send("{ESC}")

;Copy screen contents to clipboard

Send("!{PRINTSCREEN}")

MsgBox(4096, "Screen capture status", "Desired text is highlighted. Image of browser window has been copied to the clipboard.")

Func End()

MsgBox(0, "Script ended", "` key pressed. Script ended")

Exit

EndFunc ;==>End

Fantastic!

this was exactly what I needed, thanks!!

Share this post


Link to post
Share on other sites

CODE
#comments-start

.

.

.

; Select All and copy to clipboard

Send("^a^c")

; Deselect all.

Send("{LEFT}")

; Put text from clipboard into a string.

; Images are ignored.

; This will copy at least the first 1on the page.

; Note: Script tested OK with 15,000 characters.

; There's probably a limit to how many characters you can copy.

$text = ClipGet()

Brilliantly simple. Thanks!

Share this post


Link to post
Share on other sites

You can do the search with FF.au3, too:

#include <FF.au3>

$Socket = _FFConnect()
if _FFAction($Socket,"search","AutoIt") then msgbox(64,"","Found AutoIt!")

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Is there any way to read the status bar in the lower left bottom of Firefox? Where it says loading..., and then Done. I have used Window Info and it does not show as visible or hidden text. I am trying to make sure a page loads before I continue in a program.

Edited by ahha

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0