Searching for text and clicking it Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WingbtZ
Posts: 22
Joined: 09 Aug 2018, 04:41

Searching for text and clicking it

09 Aug 2018, 05:37

Hello forums,

I am kinda new to AHK, and I only have some basic scripting knowlege, so do forgive if this is a noob question. I have been trying to look this up myself, but not finding the useful answer i needed.

So to the point, I am trying to make a simple script with AHK to easaly download some PDF's that I need. I got a script working, however, using mousePos isent so useful when the link I need to click tends to move every now and then.
So I was thinking of a better way to use this script, and I was thinking, maybe if it can search for a specific text line, and click on it, everything should work as butter. However, I can't realy find a good and simple way to implement this to my code. Do belive me, I have been trying to find some tutorials and looking at the referance on AHK, but finding little help.

So this is the code I made and the part that is missing in it.. If anyone can help of point me in the right direction.
Yes I know this isent the best way to do the task I want to do, but it is a learning curve for me :) And do keep in mind that I'm new to this.

Thanks!

Code: Select all

Escape::
ExitApp
Return
X::
Send, ^c ; Copy text from excel window
Sleep, 100
Send, !{Tab} ; Tab to chrome
Sleep, 100
MouseClick, left, 950, 328 ; Clicking the search window for webpage
Sleep, 100
Send, ^a ; Select all
Sleep, 100
Send ^v ; Pasting search text from Excel
Sleep, 100
Send, {Enter} ; Search
Sleep, 4000 ; Waiting for page to load
# Something to search for the text "Technical drawings" and clicking it, sens this is a drop-down for the pdf download page, ideal if it changes the mousePos to this text aswell
Sleep, 100
MouseClick, left, pos, pos ; Clicking download link under drop-down, this line may be subject to change
Sleep, 4000 ; Waiting for page to load
Send, ^s ; Save as PDF
Sleep, 100
Send, ^v ; Paste search text as PDF name
Sleep, 100
Send, {Enter} ; Save the pdf
Sleep, 100
Send, !w ; Close tab with pdf
Sleep, 100
Send, !{Left} ; Go back to search page
Sleep, 100
Send, !{Tab} ; Tab back to Excel
Sleep, 100
Send, {Down} ; Move down a row in Excel
return
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Searching for text and clicking it  Topic is solved

09 Aug 2018, 11:54

Since you are just starting out the most intuitive way may be to do an image search for the text. If this script will only be run on a single computer (if not you will have problems getting image search to work reliably because of resolution changes) you could take a screenshot and crop the image to only include the text "technical drawings". Once the page loads you could do an imagesearch to get the coordinates of the link. Then do a mouse click on the image position. This method will fail if you change your zoom level in the browser.

Code: Select all

ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_screenWidth%,  %A_screenheight%, *5 technicalDrawings.jpg  ;the *5 allows for a little fuzz in case the cleartype font rendering is a little blurry. If you turn cleartype font off before taking a screenshot and searching you may have better luck. 
Sleep, 100
OutputVarX:= OutputVarX +10 ;offset so mouse clicks in the right spot
OutputVarY := OutputVarY +15 ;offset so mouse clicks in the right spot
MouseClick, left, %OutputVarX%, %OutputVarY%
Alternatively, you could load the page in internet explorer and use com objects to programatically click on the link.

*edit changed truetype to cleartype
WingbtZ
Posts: 22
Joined: 09 Aug 2018, 04:41

Re: Searching for text and clicking it

10 Aug 2018, 00:28

colt wrote:Since you are just starting out the most intuitive way may be to do an image search for the text. If this script will only be run on a single computer (if not you will have problems getting image search to work reliably because of resolution changes) you could take a screenshot and crop the image to only include the text "technical drawings". Once the page loads you could do an imagesearch to get the coordinates of the link. Then do a mouse click on the image position. This method will fail if you change your zoom level in the browser.

Code: Select all

ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_screenWidth%,  %A_screenheight%, *5 technicalDrawings.jpg  ;the *5 allows for a little fuzz in case the cleartype font rendering is a little blurry. If you turn cleartype font off before taking a screenshot and searching you may have better luck. 
Sleep, 100
OutputVarX:= OutputVarX +10 ;offset so mouse clicks in the right spot
OutputVarY := OutputVarY +15 ;offset so mouse clicks in the right spot
MouseClick, left, %OutputVarX%, %OutputVarY%
Alternatively, you could load the page in internet explorer and use com objects to programatically click on the link.

*edit changed truetype to cleartype
Thanks for the tip! I will look into it in the future. However I found myself a workaround using ctrl+F.

This must be the most frankenstien script I ever made tbh lol. But again, it is a learning curve, and it works to some extend. If anything i should insert a fail-safe, like if there is no PDF that the script dosent continue as it should and screws things over. Again this worked, but i had to sit and look at it work, in case if something went wrong to press Escape.

There is ALOT of things to improve on this script till next time, but for now, I'm kinda proud of what I made with little to no knowlege about AHK, thanks to the forums and great refereances.

Code: Select all

Escape::
ExitApp ; In case of error, pressing Escape to close script
Return
X::
Loop, 159 ; Loop with amount of times this needs to be done
{
Send, ^c ; Copy search text from row in active Excel window
Sleep, 100
Send, !{Tab} ; Tab window into Chrome with search page up
Sleep, 100
MouseMove, 950, 328 ; Move mouse to the search bar
Sleep, 100
MouseClick, left ; Clicking on the search bar to activate it
Sleep, 100
Send, ^a ; Mark all text, in case of old text being present
Sleep, 100
Send ^v ; Paste search text from Excel (Clipboard)
Sleep, 100
Send, {Enter} ; Enter to begin the search
Sleep, 5000 ; Longer wait time to let the page load
Send, ^f ; Activate Ctrl+F search
Sleep, 100
Send, Technical drawings ; Searching for text "Technical drawings" on page
Sleep, 100
Send, {Enter} ; Enter to get search result
Sleep, 100
Send, ^{Enter} ; Ctrl+Enter to activate drop-down window
Sleep, 100
Send, {Tab} ; Tab to navigate down the drop-down window to the download link
Sleep, 100
Send, {Enter} ; Enter to load PDF link (Opens new tab)
Sleep, 5000 ; Longer wait time to let the page load
Send, ^s ; Save PDF with "Save as"
Sleep, 1000 ; Longer wait time to let window load properly
Send, ^v ; Paste search text from Excel (Clipboard) to name the file
Sleep, 100
Send, {Enter} ; Enter to download and save the PDF
Sleep, 100
Send, ^w ; Close browser tab
Sleep, 100
Send, !{Left} ; Go back to search page
Sleep, 1000
Send, !{Tab} ; Tab back into Excel
Sleep, 100
Send, {Down} ; Move down a row in Excel
Sleep, 1000 ; Longer wait time befor the script loops back again
}
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: downstairs, OrangeCat and 183 guests