Jump to content

Read html element using DOM


Recommended Posts

I am new to AutoIt, but I am making a script where I need to no the width of the statues bar.

$statusBar= $ie.document.getElementById("status").style.width

This line will find the status bar, but I don't now how to read how big the width is.

In javascript I would have wrote:  

alert(document.getElementById("status").style.width);

 

Btw. How do I use chrome instead of Internet Explore

$ie = ObjCreate("InternetExplorer.Application")

 Thanks

Link to comment
Share on other sites

The variable ($statusBar) should hold the data you want. To see it you could use the MsgBox or ConsoleWrite. Look for them in the helpfile.

MsgBox(0, "", $statusBar)
ConsoleWrite($statusBar & @CRLF)

You probably know that but just to be sure: "The width property has effect only on block-level elements or on elements with absolute or fixed position." W3Schools

Maybe .offsetWidth and .offsetHeight could also help.

If you are going to use Autoit to automate web tasks there is a IE.au3 UDF. Microsoft chose to expose a COM interface to the DOM in IE but Chrome dont. There is a plug-in for FF that exposes a COM interface (just search the forum and you will find it). There was also a plug-in for Chrome but i belive that is no longer supported (you can also find information about it searching in the forum).

Link to comment
Share on other sites

Quote
16 minutes ago, MichaelHB said:

The variable ($statusBar) should hold the data you want. To see it you could use the MsgBox or ConsoleWrite. Look for them in the helpfile.

MsgBox(0, "", $statusBar)
ConsoleWrite($statusBar & @CRLF)

You probably know that but just to be sure: "The width property has effect only on block-level elements or on elements with absolute or fixed position." W3Schools

I saw it on the W3Schools the variable $statusBar didn't hold it. They use a js function called alert to get it out.

;this is html

<div class="progress_bar" style="width: 86%; border: none;"></div>
<div id="status" style="">Rolling in 26.97</div>

This is from the website. I made a mistake, I not trying to read the width, but it says between <div id="status"...> </div>

So I want the program to read Rolling in 20. The number change. How do I do that?

Thanks for trying.

 

Link to comment
Share on other sites

Ok. I believe that what you want is "innertext". Try something like this:

$oDiv = _IEGetObjById($oIE, "status")
If Not IsObj($oDiv) Then Exit ConsoleWrite("Error in $oDiv" & @CRLF)

$sInnerText = _IEPropertyGet($oDiv, "innertext")
If Not @error Then
    MsgBox(0, "Inner Text", "The innertext is: " & $sInnerText)
Else
    MsgBox(0, "Error", "There was an error retrieving the innertext. The error number is: " & @error)
EndIf

If that element is inside a frame you need to grab the frame first.

Link to comment
Share on other sites

4 hours ago, MichaelHB said:

 

Ok. I believe that what you want is "innertext". Try something like this:

$oDiv = _IEGetObjById($oIE, "status")
If Not IsObj($oDiv) Then Exit ConsoleWrite("Error in $oDiv" & @CRLF)

$sInnerText = _IEPropertyGet($oDiv, "innertext")
If Not @error Then
    MsgBox(0, "Inner Text", "The innertext is: " & $sInnerText)
Else
    MsgBox(0, "Error", "There was an error retrieving the innertext. The error number is: " & @error)
EndIf

If that element is inside a frame you need to grab the frame first.

 

yes that what I needed.

$statusBar= $ie.document.getElementById("status").innerHTML

ConsoleWrite($statusBar + @CRLF)

So I changed my code, but the it sayed .document.getElementById("status").innerHTML in the consol and on the next line it sayed error. What did I do wrong?

Edited by ranjith
Link to comment
Share on other sites

If you dont post the output error, the script and the url i cant know what is wrong right?! :D

First, you need to check your code (debugging) and make sure you get the right element. And how you do that you may ask, just use the IE UDF examples and the piece of code i provided as a start. As you will see the examples will have a "msgbox" or "consolewrite" to debug. Also change "+" in "ConsoleWrite($statusBar + @CRLF)" for "&". I really think you should use the IE UDF, its very easy to understand the functions (the helpfile is well documented) and they do some "checking" to validate some objects and actions.

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