Jump to content

getting a reference to a inner-div


Recommended Posts

Hi guys,

I need some help...

I'm try to get a handle for a <div> in order to click it (it has an onclick method)

the div i want to get is let's say:

<div class="innerdiv" onclick=";;;">aaa</div>

but in the html itself, it resides in another div, so the html of the page looks like this:

...<div class="outerdiv"><div class="innerdiv" onclick=";;;">aaa</div></div>...

the way i usually get html objects is using outerHtml or innerHtml or innerText and etc the following way:

$valueToSearch="innerdiv"
$oInputs = _IETagNameGetCollection ($oIE, $tagName)
For $oInput In $oInputs
    $thePropertyValue=$oInput.outerHtml
    If StringInStr($thePropertyValue, $valueToSearch) > 0 Then
        $oInput.FireEvent("onclick")
    endif
Next

but since my div is inside another div, i'm getting the reference to the outer div when i want to get the reference to the inner one...

is there a way to get the most "inner" div?

it would be great if there would be some action like outerHtml/innerHtml that will return the content of the tag definition, e.x. only the bold part:

<div class="outerdiv"><div class="innerdiv" onclick=";;;">aaa</div></div>

ideas anyone??

thanks :x

Link to comment
Share on other sites

Here's how I'd do it. I wrote a function to list all objects of tag type $sTag ("*" by default) and then filter the ones with the same class name. This then returns an array, and you can just use the first item in the array. No idea what happens if there is more than one class (e.g. "class1 class2") but that doesn't really matter for this.

#include <IE.au3>

$oIE = _IECreate("www.google.com")
$aArray = _IEGetObjByClass($oIE, "tsf-p", "div")
$o = $aArray[1]

MsgBox(0, "test", $o.innerText)


Func _IEGetObjByClass($oIE, $sClass, $sTag = "*")
    Local $aRet[1] = [0]

    Local $allHTMLTags = _IETagNameGetCollection($oIE, $sTag)
    For $o In $allHTMLTags
        If IsString($o.className) And $o.className = $sClass Then
            $aRet[0] += 1
            ReDim $aRet[$aRet[0] + 1]
            $aRet[$aRet[0]] = $o
        EndIf
    Next

    Return $aRet
EndFunc   ;==>_IEGetObjByClass
Edited by Mat
Link to comment
Share on other sites

thanks,

the truth is that i dont have a class name in there,

i just put it in the example to make it more clear :x

what i do have in there is an onclick that i'm trying to click:

<div><div onclick="blablabla">x</div></div>

any idea how to do this nicely?

i thought that i can start playing with the string, get the outerHtml and cut only the bold part here easily:

<div xxxxxxxxx>.....</div>

and search for what i want within it but i was hoping to solve it in a more elegant way! :P

Here's how I'd do it. I wrote a function to list all objects of tag type $sTag ("*" by default) and then filter the ones with the same class name. This then returns an array, and you can just use the first item in the array. No idea what happens if there is more than one class (e.g. "class1 class2") but that doesn't really matter for this.

#include <IE.au3>

$oIE = _IECreate("www.google.com")
$aArray = _IEGetObjByClass($oIE, "tsf-p", "div")
$o = $aArray[1]

MsgBox(0, "test", $o.innerText)


Func _IEGetObjByClass($oIE, $sClass, $sTag = "*")
    Local $aRet[1] = [0]

    Local $allHTMLTags = _IETagNameGetCollection($oIE, $sTag)
    For $o In $allHTMLTags
        If IsString($o.className) And $o.className = $sClass Then
            $aRet[0] += 1
            ReDim $aRet[$aRet[0] + 1]
            $aRet[$aRet[0]] = $o
        EndIf
    Next

    Return $aRet
EndFunc   ;==>_IEGetObjByClass

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...