View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tim Williams[_4_] Tim Williams[_4_] is offline
external usenet poster
 
Posts: 114
Default IE Automation - checkbox


Matt,

This seemed to work for me.

Set objTarget = objIE.document.getElementsByName("p_columns[]")

For Each Obj In objTarget
Debug.Print Obj.parentElement.previousSibling.getAdjacentText
("afterBegin")
Next Obj

Reference:
http://msdn.microsoft.com/en-us/libr...53(VS.85).aspx


Tim


On Jan 5, 11:51*am, Matthew Herbert
wrote:
Tim,

Well, this is more of a reply to myself, but I've listed the code below that
I'm currently using to get the description text for the checkbox. *I'll use
this unless there is a "better" way.

Best,

Matt

Private Sub ListCheckBoxDescriptionText()
Dim objIE As Object
Dim objTarget As Object
Dim Obj As Object
Dim strURL As String
Dim objDesc As Object
Dim strText As String

strURL = "http://www.zacks.com/screening/custom/view.php?sid=87639"
Set objIE = GetIE(strURL)

If objIE Is Nothing Then
* * Set objIE = CreateObject("InternetExplorer.Application")
* * objIE.Visible = True
* * objIE.Navigate strURL
* * WaitForLoad objIE
End If

Set objTarget = objIE.document.getElementsByName("p_columns[]")

For Each Obj In objTarget
* * Set objDesc = Obj.parentElement.parentElement
* * strText = GetFirstInnerText(objDesc.innerHTML)
* * Debug.Print strText
Next Obj

End Sub

Private Function GetFirstInnerText(strInnerHTML As String) As String
'---------------------------------------------------------------------
'INFO: * * * * *01/05/2010, Matthew Herbert
'---------------------------------------------------------------------
'PURPOSE: * * * Get the first available innerText from strInnerHTML..
' * * * * * * * Search for the "" and then the "<" and get the
' * * * * * * * text in the middle.
'
'strInnerHTML * A text string of the .innerHTML property.
'
'RETURN: * * * *- An empty string, i.e. "", if no innerText is found.
' * * * * * * * - The string between the "" and "<".
'---------------------------------------------------------------------
'NOTES: * * * * THIS IS HARDLY TESTED! *BE CAREFUL WHEN YOU USE THIS!
' * * * * * * * Intended to deal with at least the following 2
' * * * * * * * situations:
' * * * * * * * (1) <tr<td class=tdata152 Week High<...
' * * * * * * * (2) * * <td class=tdata152 Week High<...
'---------------------------------------------------------------------
Dim lngPosStart As Long
Dim lngPosEnd As Long
Dim strTemp As String

'initialize the position for the first time in the loop
' * in order to get the InStr function to behave correctly
lngPosStart = 1
lngPosEnd = lngPosStart

Do
* * lngPosStart = InStr(lngPosEnd, strInnerHTML, "", vbTextCompare)
* * If lngPosStart = 0 Then
* * * * GetFirstInnerText = ""
* * * * Exit Function
* * End If

* * lngPosEnd = InStr(lngPosStart + 1, strInnerHTML, "<", vbTextCompare)
* * If lngPosEnd = 0 Then
* * * * GetFirstInnerText = ""
* * * * Exit Function
* * End If
Loop Until lngPosEnd lngPosStart + 1

'---------------------------------------------------------------------
'get the text in between the "" and "<"

'want the position after the ""
lngPosStart = lngPosStart + 1

'want the position before the "<"
lngPosEnd = lngPosEnd - 1

'guard against the following
If lngPosStart = lngPosEnd Then
* * GetFirstInnerText = ""
* * Exit Function
End If

strTemp = Mid(strInnerHTML, lngPosStart, lngPosEnd - lngPosStart)
'---------------------------------------------------------------------

'replace any "&" with ""
strTemp = Replace(strTemp, "&", "")

'trim off any spaces
strTemp = Trim(strTemp)

GetFirstInnerText = strTemp

End Function



"Matthew Herbert" wrote:
Tim,


As always, thank you for the help. *Yes, it was a matter of checking my own
spelling. *You have helped me in the past, and I appreciate the help now. *
When I do need to do IE automation, I usually end up doing an Advanced Search
for your posts. *However, I'm still far removed from truly knowing the "best"
way to do IE automation.


For example, I don't know the best way to get the checkbox description text.
*So, if you open the webpage you see the following:
52 Week High (Question Mark Picture) [checkbox] (for which the HTML can be
found in the chain). *


I can tell that the <tr tag is prior to "52 Week High" and that a <div tag
seems to be associated with the mouseover text. *In my code, the
".parentElement.parentElement.innerText" returns "52 Week High + the
mouseover text". *I simply want "52 Week High". *I found a previous post that
had code that I thought might be helpful:


retVal = o.Document.getElementById("member-jacket").rows[0].cells[0].innerText


However, I don't know if I'm thinking about this the right way, i.e. trying
to access the text of a specific row/cell. *(I'm also assuming that HTML
tables have rows, columns, and/or cells; however, I don't know how a
rows/columns delineation -- like in an Excel spreadsheet -- is different than
the rows[0].cells[0] shown above. *Logic tells me that rows[0].cells[0] is
the first row, first cell).


Do you have advice for getting the "52 Week High" portion of text? *(Right
now, it appears that the <div text is preceeded by a double space, but I
don't want to rely on using a double space as a way of separating the <tr
text from the <div text).


Thanks,


Matt


Set objTarget = objIE.document.getElementsByName("p_columns[]")


lngCnt = 0
For Each Obj In objTarget
* * Set objDesc = Obj.parentElement.parentElement


* * 'not sure what to do here to get the <tr text
* * strText = objDesc.innerText


"Tim Williams" wrote:


Matt,


*'thought the following might work, but I get an error
*'Set objTarget = objIE.docmument.getElementsByName("p_columns[]")


Should work if you correct the typo *in "document" *;-)


Tim