Jump to content
Sign in to follow this  
qwyzux

Move Mouse to link in Internet Browser

Recommended Posts

qwyzux

Hello All,

After loading a url in my browser (by means of au script) I would like

to check if a certain string/link is present on that page. If the link is

present, I would like to move the mouse there and click on it.

Can someone please help me generate this code?

I'm not an experienced programmer. I would like to automate some

tasks. I'm new at autoit, I love it, but i can't seem to get this working.

Greetings, Ed.

Share this post


Link to post
Share on other sites
thompsonson

Hello All,

After loading a url in my browser (by means of au script) I would like

to check if a certain string/link is present on that page. If the link is

present, I would like to move the mouse there and click on it.

Can someone please help me generate this code?

I'm not an experienced programmer. I would like to automate some

tasks. I'm new at autoit, I love it, but i can't seem to get this working.

Greetings, Ed.

I've been working on creating a set of functions to easily create automated web tests. I've been doing it by element name here's one of my functions:

CODE

Func MoveToElement($strElementName)

$oTags = _IETagNameAllGetCollection ($oIE)

For $oTag In $oTags

If ($oTag.TagName = "textarea") or ($oTag.TagName = "input") or ($oTag.TagName = "a") or ($oTag.TagName = "img") or ($oTag.TagName = "span") Then

;if ($oTag.TagName = "IMG") Then

; AddReportInfo("$oTag.src: " & $oTag.src)

; AddReportInfo("$oTag.id: " & $oTag.id & " - $strElementName: " & $strElementName)

; AddReportInfo("$oTag.TagName: " & $oTag.TagName & " - $oTag.TagName: " & $oTag.TagName)

;EndIf

$bMatch = False

if ($oTag.TagName = "span") then

$bMatch = (String($oTag.id) = $strElementName)

Else

$bMatch = (String($oTag.name) = $strElementName) or (String($oTag.id) = $strElementName)

EndIf

If $bMatch Then

$x = _IEfindPosX($oTag)

$y = _IEfindPosY($oTag)

; Need to compare the position of the element to the screen size etc... as the element could be off the screen

; get a page down thing and then call the MoveTOElement Function again

If ($oTag.TagName = "input") or ($oTag.TagName = "img") then

if $oTag.width = 00 Then

; arbitary value...

$width = 10

Else

$width = $oTag.width

EndIf

if $oTag.height = 00 Then

; arbitary value...

$height = 10

Else

$height = $oTag.height

EndIf

Else

$width = 10

$height = 10

EndIf

$windowleft = $oIE.document.parentwindow.screenLeft

$windowtop = $oIE.document.parentwindow.screenTop

;check the co-ordinates are within the IE window...

$windowheight = _IEPropertyGet ( $oIE, "height" )

$windowwidth = _IEPropertyGet ( $oIE, "width" )

;AddReportInfo("Width: " & $windowwidth & " - x: " & $x)

;AddReportInfo("Height: " & $windowheight & " - y: " & $y)

if ($y > ($windowheight - 60)) Then ; ($x > ($windowwidth - 20)) Or

; we need to scroll to it

$oIE.document.parentwindow.eval("self.scrollTo(0," & $y & ")") ; " & $x & "

;click on the new co-oridnates...

$offsetY = $oIE.document.parentwindow.eval("document.body.scrollTop")

$offsetX = $oIE.document.parentwindow.eval("document.body.scrollLeft")

;AddReportInfo("$offsetX = " & $offsetX)

;AddReportInfo("$offsetY = " & $offsetY)

;AddReportInfo("$windowleft = " & $windowleft)

;AddReportInfo("$windowtop = " & $windowtop)

; get the required mouse co-ordinates and move them there

$mousex = $windowleft + ($x - $offsetX) + int($width/2)

$mousey = $windowtop + ($y - $offsetY) - int($height/2)

;AddReportInfo("$mousex = " & $mousex)

;AddReportInfo("$mousey = " & $mousey)

LogStep("MoveMouseTo", "x,y", $mousex & "," & $mousey)

MoveMouseTo($mousex, $mousey)

;MoveToElement($strElementName)

Else

;AddReportInfo("x and y are in the viewable window.")

; they are not greater so head straight there

; get the required mouse co-ordinates and move them there

$mousex = $windowleft + $x + int($width/2)

$mousey = $windowtop + $y + int($height/2)

LogStep("MoveMouseTo", "x,y", $mousex & "," & $mousey)

mousemove($mousex,$mousey)

EndIf

;exit the loop

ExitLoop

EndIf

EndIf

Next

$params = "$strElementName"

$Values = $strElementName

LogStep('MoveToElement', $params, $Values)

EndFunc

As you can see it will only go to the Tags: textarea, input, a, img and span. This is because not all tags have a name or an id attribute and AutoIT will bomb... not good!

This code is adapted from other code on this forum. Have a search around as there is some good information there.

Matt

Share this post


Link to post
Share on other sites
qwyzux

Thanks Matt,

I'll check this out (it looks very difficult :rolleyes: )

Ed.

Share this post


Link to post
Share on other sites
qwyzux

Matt,

I can't get it to run. It seems that I cannot retrieve the x position and

y position.

I'll have to really get more into it.

In the meanwhile, is there anybody that has an easy way of doing this?

Ed.

Share this post


Link to post
Share on other sites
GMK

You'll need to include the following line before the function:

#include <IE.au3>

Also, look at the help file for _IELinkClickByIndex and _IELinkClickByText.

Share this post


Link to post
Share on other sites
qwyzux

Thank you GMK!

I try some more and read more about the specific functions :rolleyes:

Ed.

Share this post


Link to post
Share on other sites
yyfy105

Something are need to updated in the MoveToElement for AutoIT Version 3.

1. Use the "mousemove" to instead the "MoveMouseTo", no this default function: MoveMouseTo if use AutoIT Version 3.

2. Remove the "LogStep", no this default function: "LogStep" if you are use AutoIT Version 3.

3. Add the _IEfindPosX, _IEfindPosY into the script files

the detail of the 2 functions in http://www.autoitscript.com/forum/index.ph...p;hl=offsetLeft

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.