Is there a way to search for text on a webpage and move the mouse cursor to that position?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Is there a way to search for text on a webpage and move the mouse cursor to that position?

02 May 2021, 10:59

On Bing's homepage, there's a hyperlink that says "Learn More" at the bottom of the page. When you click on it, there's an explanation of the daily picture shown on their homepage. (sometimes 1 paragraph, sometimes longer than that). I'd like to copy that text (explanation) into clipboard memory. Is this possible to do with AHK?

If that's not possible, then at the very least, I'd like AHK to move my mouse cursor to that link and click it.

Firefox is my primary browser. Thanks.
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

02 May 2021, 12:40

M4verick wrote: I'd like to copy that text (explanation) into clipboard memory. Is this possible to do with AHK?
Try the code below. Press Ctrl+F1 to copy the text to the clipboard. You don't need the Bing page open in any browser for it to work.

Code: Select all

^F1::
	BingHtml := URLDownloadToVar("https://bing.com")
	RegExMatch(BingHtml, "(?<=class=""vs_bs_title"">).*?(?=<)", title)
	RegExMatch(BingHtml, "(?<=<span class=""text"" id=""iotd_desc"">).*?(?=<)", text)
	Clipboard := title "`n`n" text
	MsgBox, 64, Copied to Clipboard, The Bing 'Image of the Day' explanation has been copied to the clipboard., 3
return

URLDownloadToVar(url){
	hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
	hObject.Open("GET",url)
	hObject.Send()
	return hObject.ResponseText
}
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

02 May 2021, 13:09

Sorry, but it didn't work. Got the following error message.
AHK error msg.png
AHK error msg.png (50.67 KiB) Viewed 2264 times
Based on the error message, I took a guess and placed the missing bracket here:

Code: Select all

	hObject.Open("GET",url)
        }
When it ran it a 2nd time, I didn't get an error message but nothing got copied.
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

02 May 2021, 13:17

Copy the complete code from the code box, not just the part that is visible in collapsed/default view.
You can use 'Select all', 'Expand View' or scroll to select/see the whole code.
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

02 May 2021, 13:24

gregster wrote:
02 May 2021, 13:17
Copy the complete code from the code box, not just the part that is visible in collapsed/default view.
You can use 'Select all', 'Expand View' or scroll to select/see the whole code.
Yeah I got it now. I just realized my error as I was reviewing the code (in my copied file). Thanks guys.
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

04 May 2021, 10:56

boiler wrote:
02 May 2021, 12:40
Try the code below. Press Ctrl+F1 to copy the text to the clipboard. You don't need the Bing page open in any browser for it to work.
Hi Boiler, the first code you gave me didn't quite work out. It works, but it copies the first paragraph only - it's missing the 2nd paragraph. For example, if you look at the description printed for today, it says:
Happy Star Wars Day!

Sprinting over the sands, this grey seal reminds us of Luke Skywalker's zippy landspeeder vehicle from the first 'Star Wars' flick. But unlike Luke, it's not bound for Tosche Station to pick up some power converters—just to the sea for a fishy snack.
But there is actually more. If you click on the link that says "Learn More"

It will reveal the entire text.
Happy Star Wars Day!

Sprinting over the sands, this grey seal reminds us of Luke Skywalker's zippy landspeeder vehicle from the first 'Star Wars' flick. But unlike Luke, it's not bound for Tosche Station to pick up some power converters—just to the sea for a fishy snack.

It looks like we're on Luke's homeworld of Tatooine for Star Wars Day (May the Fourth be with you!). But this is really a beach on England's east coast, far, far away from the Tunisian desert where the 1977 film was shot. As for our seal friend, it was mid-flop just an inch off the ground at the moment this photo was taken. The photo's focus and perspective fool your eye, making the seal seem to hover like Luke's souped-up speeder. Quite the Jedi mind trick!
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

04 May 2021, 12:37

This is the best I can do right now, which is to get it to automatically open the page that has that info on it. When I try to download the source HTML for that page, it gives me search results that don't include that info. I could probably do some brute force stuff to get the text off the page that opens in browser, but that might not be better than just seeing the page since it sounds like that's the main thing you're looking for. Press Ctrl+F1, and it will open the page with the full explanation, and you don't need anything to be already open when you press the hotkey.

Code: Select all

^F1::
	BingHtml := URLDownloadToVar("https://bing.com")
	RegExMatch(BingHtml, ".{300}(?=Learn more<\/a>)", BingHtmlChunk)
	RegExMatch(BingHtmlChunk, "(?<=href="")[^""]+", LearnMoreURL)
	LearnMoreURL := StrReplace(LearnMoreURL, "&amp;", "&")
	LearnMoreURL := "https://www.bing.com" . StrReplace(LearnMoreURL, "&quot;", """")
	Run, % LearnMoreURL
return

URLDownloadToVar(url){
	hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
	hObject.Open("GET",url)
	hObject.Send()
	return hObject.ResponseText
}
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 09:39

Hi Boiler, I appreciate your effort but it's not working.

I tried your most recent code & got the following:

https://i.postimg.cc/FsRcDwzv/bing-sr-incorrect.png

What should be shown when you manually click on the "Learn More" link, you should see this:

https://i.postimg.cc/9fGqDh8d/bing-sr-correct.png

Interestingly enough, the only difference between the URL is the quotation in the 2nd screenshot:

https://www.bing.com/search?q=national+nurses+week&filters=HpDate:"20210506_0700"

When i ran your script, I noticed the URL did not contain the quotation marks wrapped around the date.
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 09:58

Yes, I know what it's supposed to show, and it works on my machine. It shows the exact page you indicated it's supposed to show. Mine produces the complete URL including the quotation marks wrapped around the date.

To try to troubleshoot, replace Run in the script with MsgBox, and see what it shows when you run it and press the hotkey. You can press Ctrl+C to copy the contents of the MsgBox when it is showing so you can paste it here. Mine copied directly from that MsgBox shows this:
https://www.bing.com/search?q=national+nurses+week&form=hpcapt&filters=HpDate:"20210506_0700"
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:06

boiler wrote:
06 May 2021, 09:58
To try to troubleshoot, replace Run in the script with MsgBox, and see what it shows when you run it and press the hotkey.
I got this:

https://i.postimg.cc/V6vgnvxy/Message-box.png
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:09

Can you just post it as text rather than a picture that can't be clicked on or copied as text? I told you how you can copy the contents of the MsgBox.
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:19

OK, so now I put that in a proper link below. When you click on it from your machine, does it bring up the proper page? It does on my machine.

https://www.bing.com/search?q=national+nurses+week&form=hpcapt&filters=HpDate:"20210506_0700"
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:20

I was wrong. There are actually 2 differences in the URLs. One is the quotation marks (already mentioned above) and also when I click on the link manually, this is absent in the URL.

Code: Select all

&form=hpcapt
Last edited by M4verick on 06 May 2021, 10:21, edited 1 time in total.
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:21

boiler wrote:
06 May 2021, 10:19
OK, so now I put that in a proper link below. When you click on it from your machine, does it bring up the proper page? It does on my machine.

https://www.bing.com/search?q=national+nurses+week&form=hpcapt&filters=HpDate:"20210506_0700"
Yes it does.
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:23

Then I don't see why it wouldn't work. Can you try this as a stand-alone script and see if it brings up the proper page?:

Code: Select all

Run, https://www.bing.com/search?q=national+nurses+week&form=hpcapt&filters=HpDate:"20210506_0700"
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:33

I figured it must be the difference in browsers, and sure enough, when I run it in Edge, I get what you get. For some reason, Edge modifies the URL. I suppose you are using Edge as your default browser. If you have Chrome installed, change the the Run line in the script to the following:

Code: Select all

	Run, % "chrome.exe " LearnMoreURL
M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:36

boiler wrote:
06 May 2021, 10:33
I figured it must be the difference in browsers, and sure enough, when I run it in Edge, I get what you get. For some reason, Edge modifies the URL. I suppose you are using Edge as your default browser. If you have Chrome installed, change the the Run line in the script to the following:

Code: Select all

	Run, % "chrome.exe " LearnMoreURL
Nope, Firefox is my default browser of choice.

I don't have Google Chrome installed but I do have Vivaldi which is Chrome based I think. I'll be right back.

EDIT:

I just tried Vivaldi browser and the URL in the address bar was... LOL

http://learnmoreurl
Last edited by M4verick on 06 May 2021, 10:42, edited 1 time in total.
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Is there a way to search for text on a webpage and move the mouse cursor to that position?

06 May 2021, 10:42

OK. It's the same issue with Firefox. There may be a way to run it as if you pasted it into the URL entry field rather than whatever it (and Edge) does to modify it, but I don't know how to do that. One workaround I can think of would be to add that link to a GUI and have the script click on its own link then close the GUI. So a small GUI would just briefly flash on the screen before opening the browser window.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: anogoya, Descolada, Google [Bot], Mannaia666, skeerrt and 149 guests