Jump to content

how to get a IE tag value by class name ?


Recommended Posts

Hi.

I'm new to autoit, but I have some scripting experience.

I'm trying to get the value of a DIV tag from a html page. This DIV tag has no "id" or "name" attributes so I can't use _IEGetObjByName or _IEGetObjById.

My only option is to search the DIV tag by "class" attribute....

If anyone can help me with this, thanks in advance.

Edited by Sanchezu
Link to comment
Share on other sites

This is the code I'm using:

#include <IE.au3>
$oIE = _IECreate ("http://www.test.com", 0, 0, 1, 0)

$tags = $oIE.document.GetElementsByTagName("div")
For $tag in $tags
    $class_value = $tag.GetAttribute("class")
    If $class_value = "levelBar" Then
        MsgBox(0, "Level: ", "Level found :)")
    EndIf
Next

_IEQuit ($oIE)

I get no msg box, so I'm wondering why it cannot find the div tag with class attribute ="levelBar"

Edited by Sanchezu
Link to comment
Share on other sites

IE has problems with that func. use className instead:

$class_value = $tag.className

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

  • 3 years later...

Use

If String($class_value) = "levelBar" Then

If there is no Classname, 0 is returned. This forces a numeric comparison instead of the string one you want... it converts your string to 0 and 0 = 0.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Assuming you want to click the DIV you were searching for above (though most buttons are <inputs>)

#include <IE.au3>
$oIE = _IECreate ("http://www.test.com", 0, 0, 1, 0)
 
$oCorrectObj = ""

$tags = $oIE.document.GetElementsByTagName("div")
For $tag in $tags
$class_value = $tag.GetAttribute("class")
If string($class_value) = "levelBar" Then
    $oCorrectObj = $tag

MsgBox(0, "Level: ", "Level found :)")
    ExitLoop

EndIf
Next
 
If IsObj ( $oCorrectObj ) Then
 
_IEAction ($oCorrectObj, "click")


EndIF


_IEQuit ($oIE)

edit: just realized this is a super old post, but the same structure can be used for whatever your element is

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • 4 years later...

_IENavigate($object, 'javascript:document.querySelector(".classname").href')

easiest method is by far the above. my one lline example simply evals javascript to select the class then returns the href. i know this posst is old but i figured someone might bee interested to know this can be done in one line

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...