George
George

Reputation: 4674

AutoHotKey How to get the text from a web page in Internet Explore

I have to do some status request to a 3rd party vendor website at work. It is 500-600 times per day. I am trying to automate this task. Currently my code uses the following method.

; Helper function to get the text from current web page
CurrentScreen() {
    Sleep, 500
    MouseClick, left,  880, 240
    Send, {CTRLDOWN}a{CTRLUP}
    Sleep, 500
    Send, {CTRLDOWN}c{CTRLUP}
    Sleep, 500
    return Clipboard
}

; Helper function to read at line number X and return text
GetInformation(webpageBuffer) {
   If (A_Index == 30)
      ; Do something here and return data
}

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate("www.someVendor.com")
IELoad(wb)

; Do a lot of clicking and searching
someText := GetInformation(CurrentScreen())

I searched online and found URLDownloadToFile and I read the documentation here. But this does not work, because the vendor website information I need is requested via a form, it does not have a static website link I can use.

My current method work, but it is rather slow, since I have am using additional 2-3 seconds on for each page I have to read, and my program will flash blue and white while it is running (from the copy and paste). Is there other solution to read text from an Internet Explorer page load, that does not use the copy, paste, read clipboard method?

Upvotes: 2

Views: 5456

Answers (2)

Forivin
Forivin

Reputation: 15508

You could go even further and communicate with the API directly. This is way faster, more reliable and can run completely hidden in the background without affecting the user experience.

You just need to find out 3 things:

  • What is the form submitting?
  • What method does the form use? (POST, GET, ...)
  • To which URL is the form submitting the data?

You can find all that that out by looking at the HTML code or by actually recording the form submit using the developer tools of the browser or using add ons like postman or using standalone programs like Fiddler.

Here is an example of what it could look like in the end:

formTargetUrl := "www.someVendor.com"
formMethod := "POST"
fromData := "exampleKey=exampleValue&[email protected]&foo=bar"

HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpObj.Open(formMethod,formTargetUrl)
HttpObj.Send(fromData)

rawHtmlResponse := HttpObj.ResponseText

;now you could use regex to find the text, 
;you could also dump rawHtmlResponse to a text file, 
;or you could use the HTMLfile object if you can identify the html element by its id, name, class, tagname, etc.

document := ComObjCreate("HTMLfile")
document.write(rawHtmlResponse)

textElement := document.getElementById("someId")
;you may also try:
;textElement := document.getElementsByName("someName")[1] ;returns multiple results (not sure if 1 or 0 is the first result)
;textElement := document.getElementsByTagName("someTagName")[1] ;returns multiple results (not sure if 1 or 0 is the first result)
;textElement := document.getElementsByClassName("someClassName")[1] ;returns multiple results (not sure if 1 or 0 is the first result)

MsgBox % textElement.innerText
;you may also try
;MsgBox % textElement.textContent
;MsgBox % textElement.innerHTML
;MsgBox % textElement.value

You also might wanna look at this: How to do logins using the WinHttpRequest COM?

Upvotes: 3

PGilm
PGilm

Reputation: 2312

If you know the exact element name that contains the text you want, use

var := wb.Document.getElementByID(Element_Name).innerText

to retrieve the text. If you don't know the variable name, but know the index number, i, or can loop through the index numbers looking for some text in particular, there is something similar:

wb.document.all[i].innerText

Hth,

Upvotes: 2

Related Questions