Sign in to follow this  
Followers 0
icu

How to get a string variable from .innertext and .innerHTML? [solved]

16 posts in this topic

#1 ·  Posted (edited)

Dear AuotIt Community,

I don't understand how AutoIt can show text from an object with .innertext or .innerHTML with ConsoleWrite but as soon as I try to put that text in a string with something like:

$sText = $o.innertext

or

$sText = $o.innerHTML

I get an error "==> Variable used without being declared.:"

The operations are in a nestled loop but that shouldn't matter should it?

For example, this is what my erroneousness code kind of looks like:

If $i = 1 Then $sText = $o.innertext
If $i = 2 Then $sHTML = $o.innerHTML
ConsoleWrite("Text: " & $sText & " HTML: " & $sHTML)

Thanks for any and all help,

icu

Edited by icu

Share this post


Link to post
Share on other sites



Do you use IE.au3 or something else ?


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

If $i = 1 Then  ConsoleWrite("Text:"& $o.innertext)

Try like this.

Edited by Juvigy

Share this post


Link to post
Share on other sites

Hi wakillon and Juvigy,

@wakillon:

Yes I do use #Include <IE.au3> for the script I'm working on. As I understand it the .innertext and .innerHTML bits are COM references (I've only just started learning about them). I would have thought that $sText = $o.innerHTML would tell AutoIt to put the HTML string into variable £sText but it seems like it doesn't.

@Juvigy

I've done what you suggested before posting, the problem I have is that I need to use the string from $o.innertext in several other functions and it will change as the script completes. This is why I wanted to extract the string out of $o.innertext at various points and put it into a separate variable. Also I need to regex the id number out of the object's .innerHTML as $o.ID doesn't work. I've worked out the expression that will do this: "=(.*)\stype" however I can't get AutoIt to the part where I can extract it from the string as the string variable I the error "==> Variable used without being declared.:"

Ideally I would want something like the following bit of code to work but it doesn't:

If $i = 4 Then $sID = StringRegExp ($o.innerHTML, "=(.*)\stype", 1)

Share this post


Link to post
Share on other sites

Like this ?

#include <IE.au3>

$Url='http://hotfile.com/'
$oIE = _IECreate ( $Url, 0, 1, 1 )
$oForms = _IEFormGetCollection ( $oIE )
For $oForm In $oForms
    $_innertext = $oForm.innertext
    ConsoleWrite ( "$_innertext : " & $_innertext & @CRLF )
Next
_IEQuit ( $oIE )

Show your script...


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Show your code and error line - you must have forgoten to declare some variable local/global.

Share this post


Link to post
Share on other sites

If you're getting a "Variable used without being declared error" then you're simply not declaring your variable.

Even wakillon's example below would fail if

Opt("MustDeclareVars", 1)
was used or
#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
was used in the script at the top.

How to declare a variable: http://www.autoitscript.com/autoit3/docs/intro/lang_variables.htm

Like this ?

#include <IE.au3>

$Url='http://hotfile.com/'
$oIE = _IECreate ( $Url, 0, 1, 1 )
$oForms = _IEFormGetCollection ( $oIE )
For $oForm In $oForms
    $_innertext = $oForm.innertext
    ConsoleWrite ( "$_innertext : " & $_innertext & @CRLF )
Next
_IEQuit ( $oIE )

Show your script...


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Thanks wakillon for your quick reply.

I don't really want to ConsoleWrite the strings, I just want to use it to make sure the strings are in the variables because I'm going to do a lot of operations on those variables.

I can't put the whole script here for privacy reasons, but the important bit I'm stuck on is below:

#Include <IE.au3>
#Include <Array.au3>

Const $sURL = "http://www.anysite.com"
$oIE = _IECreate ($sURL)

$nTable_Number = 5
$oTable = _IETableGetCollection ($oIE, $nTable_Number)

$iRow_Count = 0
$iCell_Count = 0

For $oEach_Row In $oTable.rows
    For $oEach_Cell In $oEach_Row.cells
        ; Skip the first row
        If $iRow_Count = 0 Then $iCell_Count += 6
        ; If $iCell_Count = 1 -> Extract the string and put it in $sRef
        If $iCell_Count = 1 Then $sRef = $oEach_Cell.innertext
        ; If $iCell_Count = 4 -> Extract the link object id from $oEach_Cell.innerHTMLinto $sLink_id
        If $iCell_Count = 4 Then $sLink_HTML = $oEach_Cell.innerHTML
            $sLink_ID = StringRegExp ($sLink_HTML "=(.*)\sonclick", 1)
        ; If $iCell_Count = 5 -> Extract the checkbox object id from $oEach_Cell.innerHTMLinto $sCheckbox_id
        If $iCell_Count = 5 Then $sCheckbox_HTML = $oEach_Cell.innerHTML
            $sCheckbox_ID = StringRegExp ($sCheckbox_HTML, "=(.*)\stype", 1)
        ConsoleWrite ("Row#: " & $iRow_Count & " Ref: " & $sRef & " Link ID: " & $sLink_ID & " Checkbox ID: " & $sCheckbox_ID)
        $iCell_Count += 1
    Next
    $iRow_Count += 1
    $iCell_Count = 0
Next
$iRow_Count = 0

On the line with ConsoleWrite I intend to replace it with a lot more code but I can't get any further unless I can extract the text properly from $oEach_Cell.

Any ideas?

Many thanks for your help.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ok, but which variable return error message ?

Edit : or add Global $sRef, $sLink_HTML, $sCheckbox_HTML

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thanks to all for your help but I'm still struggling to progress with my script even with the advice you have given.

@SmOk_N:

Thanks for your reply. I didn't know about Opt, or the switches, thank you for the pointer! I've read the Help file for Opt and "Opt("MustDeclareVars", 0)" is the default AutoIt setting. I still put "Opt("MustDeclareVars", 0)" under all my #Include <*.au3> lines and then ran my script but it still has the error "Variable used without being declared."

Also, as far as I know, 'If <expression> Then statement' isn't a function. The the language reference link you gave me does state:

If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.

However when I look up the list of functions here, the 'If <expression> Then statement' isn't listed there.

@wakillon:

Thanks again for replying. I've added Global to the variable assignment and I still get the error. Please see the code example 1 below.

@Juvigy:

Thanks for your reply. Please see the code below and the 'cut and paste' job from the console. I actually tried declaring the variables as blank before the loop and got nowhere.

Code 1: With declared variables

#Include <IE.au3>
#Include <Array.au3>

Const $sURL = "http://www.anysite.com"
$oIE = _IECreate ($sURL)

$nTable_Number = 5
$oTable = _IETableGetCollection ($oIE, $nTable_Number)

$iRow_Count = 0
$iCell_Count = 0

; Declared variables
Global $sSupplier_Ref
Global $sLink_HTML
Global $sCheckbox_HTML

For $oEach_Row In $oTable.rows
    For $oEach_Cell In $oEach_Row.cells
        ; Skip the first row
        If $iRow_Count = 0 Then $iCell_Count += 6
        ; If $iCell_Count = 1 -> Extract the string and put it in $sSupplier_Ref
        If $iCell_Count = 1 Then $sSupplier_Ref = $oEach_Cell.innertext
        ; If $iCell_Count = 4 -> Extract the html string  from $oEach_Cell.innerHTML
        If $iCell_Count = 4 Then $sLink_HTML = $oEach_Cell.innerHTML
        ; If $iCell_Count = 5 -> Extract the checkbox html string from $oEach_Cell.innerHTML
        If $iCell_Count = 5 Then $sCheckbox_HTML = $oEach_Cell.innerHTML
        ; I then needed to do a few things with the variables like regex the ID out of the html and planned on using ConsoleWrite to check that the text was properly in each variable as the loop completed 
        ConsoleWrite ("Row#: " & $iRow_Count & "Supplier Ref: " & $sSupplier_Ref & " Link HTML: " & $sLink_HTML & " Checkbox HTML: " $sCheckbox_HTML)
        $iCell_Count += 1
    Next
    $iRow_Count += 1
    $iCell_Count = 0
Next
$iRow_Count = 0

\file.au3 (167) : ==> Error in expression.:

ConsoleWrite ("Row#: " & $iRow_Count & "Supplier Ref: " & $sSupplier_Ref & " Link HTML: " & $sLink_HTML & " Checkbox HTML: " $sCheckbox_HTML)

ConsoleWrite (^ ERROR

>Exit code: 1 Time: 3.474

Code 2: Without declared variables

#Include <IE.au3>
#Include <Array.au3>

Const $sURL = "http://www.anysite.com"
$oIE = _IECreate ($sURL)

$nTable_Number = 5
$oTable = _IETableGetCollection ($oIE, $nTable_Number)

$iRow_Count = 0
$iCell_Count = 0

#cs No declared variables
Global $sSupplier_Ref
Global $sLink_HTML
Global $sCheckbox_HTML
#ce

For $oEach_Row In $oTable.rows
    For $oEach_Cell In $oEach_Row.cells
        ; Skip the first row
        If $iRow_Count = 0 Then $iCell_Count += 6
        ; If $iCell_Count = 1 -> Extract the string and put it in $sSupplier_Ref
        If $iCell_Count = 1 Then $sSupplier_Ref = $oEach_Cell.innertext
        ; If $iCell_Count = 4 -> Extract the html string  from $oEach_Cell.innerHTML
        If $iCell_Count = 4 Then $sLink_HTML = $oEach_Cell.innerHTML
        ; If $iCell_Count = 5 -> Extract the checkbox html string from $oEach_Cell.innerHTML
        If $iCell_Count = 5 Then $sCheckbox_HTML = $oEach_Cell.innerHTML
        ; I then needed to do a few things with the variables like regex the ID out of the html and planned on using ConsoleWrite to check that the text was properly in each variable as the loop completed 
        ConsoleWrite ("Row#: " & $iRow_Count & "Supplier Ref: " & $sSupplier_Ref & " Link HTML: " & $sLink_HTML & " Checkbox HTML: " $sCheckbox_HTML)
        $iCell_Count += 1
    Next
    $iRow_Count += 1
    $iCell_Count = 0
Next
$iRow_Count = 0

file.au3 (163) : ==> Variable used without being declared.:

ConsoleWrite ("Row#: " & $iRow_Count & "Supplier Ref: " & $sSupplier_Ref & " Link HTML: " & $sLink_HTML & " Checkbox HTML: " $sCheckbox_HTML)

ConsoleWrite ("Row#: " & $iRow_Count & "Supplier Ref: " & ^ ERROR

>Exit code: 1 Time: 17.044

Thanks to everyone again for any and all help.

Kind regards,

icu

Edited by icu

Share this post


Link to post
Share on other sites

hi,

have been a bit busy, but from a quick look at your error, it seems to be because there should be an ampersand here:

ConsoleWrite ("Row#: " & $iRow_Count & "Supplier Ref: " & $sSupplier_Ref & " Link HTML: " & $sLink_HTML & " Checkbox HTML: "& $sCheckbox_HTML)

hope to be of more help soon

-smartee

Share this post


Link to post
Share on other sites

If $i = 1 Then $sText = $o.innertext
If $i = 2 Then $sHTML = $o.innerHTML
ConsoleWrite("Text: " & $sText & " HTML: " & $sHTML)
This code is bound to cause errors because when $i=1, only $sText is declared(by assignment) however an attempt is made to read $sText and $sHTML when the ConsoleWrite is executed. Why not use something like:
$sHTML=""

If $i = 1 Then $sText = $o.innertext
If $i = 2 Then $sHTML = $o.innerHTML
ConsoleWrite("Text: " & $sText & " HTML: " & $sHTML)
-smartee

Share this post


Link to post
Share on other sites

In waiting for the next error...Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

@smartee:

Well you were right, sheepishly I admit that I missed out the '&'. After that I used your declare example '$sHTML=""' and things worked fine... well kind off. I've got errors, just a new kind, so before I go running to the forum I'm learning to meticulously check my code before posting...

Moral of the story:

  • Check your code for incorrect syntax before posting on a public forum and looking like a noob, and
  • Declare variables as empty outside of a loop before putting information into them while performing a loop.

I still don't understand why variables declared in 'If <expression> Then statement' are not accepted by AutoIt and I don't think the AutoIt help documentation states this fact clearly (or clearly enough).

Thank you once again for your insight.

@wakillon:

Well yes, I'm sure I'll have more errors :-) The great thing about the AutoIt community on this forum seems to be a genuine helpful environment matched with a vast depth of knowledge. I can only hope to learn more to help others.

Share this post


Link to post
Share on other sites

hi again icu :unsure:,

I still don't understand why variables declared in 'If <expression> Then statement' are not accepted by AutoIt..

Actually, variables declared within If..Then conditions are fine, but you must also consider the case where the condition is not satisfied, and as such the variable will never be declared, for example:
If Mod(@MDAY, 2) = 0 Then $sIsDayEven = "Today is even."
ConsoleWrite($sIsDayEven) ; This line will cause an error on "odd" days, because $sIsDayEven is declared only on "even" days

The obvious workaround is to wrap all references to the variable in the same condition, like:

If Mod(@MDAY, 2) = 0 Then
    $sIsDayEven = "Today is even."
    ConsoleWrite($sIsDayEven) ; No error this time
EndIf

But what if we don't want to read it in the same condition? Then do something such as:

If Mod(@MDAY, 2) = 0 Then $sIsDayEven = "Today is even."
$i = 1 ; Other processing
If IsDeclared("sIsDayEven") Then ConsoleWrite($sIsDayEven) ; Test if variable is declared first

Hope this clears some things up :>

Glad to help ;),

-smartee

Share this post


Link to post
Share on other sites

icu,

Declare variables as empty outside of a loop before putting information into them while performing a loop.

I take this fallacy to be a result of your previous misconception with conditional declarations.

Here's a simple example illustrating how to safely declare and modify a variable's data within a loop structure :unsure:

For $i = 1 To 100
    If Not IsDeclared("iSum") Then Local $iSum = 0
    $iSum += $i
Next
MsgBox(64, @ScriptName, "The sum of all integers from 1 to 100 is equal to " & $iSum)

-smartee

Share this post


Link to post
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
Sign in to follow this  
Followers 0