Jump to content

how to change an object style in a web page?


 Share

Go to solution Solved by dragan,

Recommended Posts

Hello,

how can I change the "style" property of an <img> tag?
i tried with this short example, but without sucess.
what am I doing wrong?

#include <IE.au3>
$oIE = _IECreate()
; Here I create a web page with an image inside, with the style="border:5px solid green"
Local $sHTML = '<html><body><img src="http://aut1.autoit-cdn.com/site/wp-content/themes/TheCorporation/images/logo@2x.png" width="210"height="72" alt="AutoItScript" id="logo" style="border:5px solid green" /></body></html>'
_IEBodyWriteHTML($oIE, $sHTML)
Sleep(3000) ; a pause and then I remove the border
; here I change the body content like the above but without the border (without style="border:5px solid green")
Local $sHTML = '<html><body><img src="http://aut1.autoit-cdn.com/site/wp-content/themes/TheCorporation/images/logo@2x.png" width="210"height="72" alt="AutoItScript" id="logo" /></body></html>'
_IEBodyWriteHTML($oIE, $sHTML)

; now I would like to set the border again around the image
; but without success

Local $oImg = _IEImgGetCollection($oIE, 0)
Local $bodyImg = _IEDocGetObj($oImg)

; --- zone of mistakes ----

$bodyImg.style = "border:5px solid green" ; <-- this fails
; : ==> The requested action with this object has failed.:

_IEPropertySet($bodyImg, "style", "border:5px #FF0000") ; ; <-- this fails
;--> IE.au3 V2.4-0 Error from function _IEPropertySet, $_IEStatus_InvalidValue (Invalid Property)


$oImg.style = "border:5px solid green" ; <-- this fails
_IEPropertySet($oImg, "style", "border:5px #FF0000") ; ; <-- this fails

any help is welcome
thanks

Edited by Pincopanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

You need to set the "style" attribute to your image object (answer lies here: http://msdn.microsoft.com/en-us/library/ie/ms536739%28v=vs.85%29.aspx )

#include <IE.au3>
$oIE = _IECreate()



Local $sHTML = '<html><body><img src="http://aut1.autoit-cdn.com/site/wp-content/themes/TheCorporation/images/logo@2x.png" width="210"height="72" alt="AutoItScript" id="logo" /></body></html>'
_IEBodyWriteHTML($oIE, $sHTML)









Local $AllImages = _IETagNameGetCollection($oIE, 'img')
For $OneImage in $AllImages
    If $OneImage.id = 'logo' Then
        MsgBox(0, '', 'about to change')
        $OneImage.setAttribute("style", "border:5px solid green;")
        MsgBox(0, '', 'after changing')
    EndIf
Next
Edited by dragan
Link to comment
Share on other sites

Thanks for answers

@aberration
I also have seen the "short" html listing, but if you press F12 developer tools (I have IE8) you will see the whole html listing there. Strange behavior :think:

@dragan
well, I see that also running your script, no success is achieved, but, again, if you press F12 in the browser, is possible to note that before the setAttribute() statement, no style argument is present in the listing, but after the setAttribute(), this time is present the style attribute in the page source, but it is empty like this -> style="". Strange behavior again :huh2:
also, reading at the link you provided, it says: AttributeValue Type:Variant, how can it be passed as variant?

thanks to both for suggestions.

..... my doubts still remains :blink:

edit:

@aberration

I have also tried it on real html pages, but with the same result.

Edited by Pincopanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

It works for me, win 7, 64-bit, ie v10

 

it doesn't for me win xp-sp3 32-bit ie v8 :(

some workarounds ? :rolleyes:

edit:

autoit v3.3.8.1

IE.au3 V2.4-0

Edited by Pincopanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

There's always a workaround:
 

#include <IE.au3>
$oIE = _IECreate()
Local $sHTML = '<html><body><img src="http://aut1.autoit-cdn.com/site/wp-content/themes/TheCorporation/images/logo@2x.png" width="210"height="72" alt="AutoItScript" id="logo" /></body></html>'
_IEBodyWriteHTML($oIE, $sHTML)
Local $AllImages = _IETagNameGetCollection($oIE, 'img')
For $OneImage in $AllImages
    If $OneImage.id = 'logo' Then
        If $OneImage.getAttribute("style") = '' Then
            MsgBox(0, '', 'about to change')
            $OneImage.outerhtml = StringTrimRight($OneImage.outerhtml, 1) & ' style="border:5px solid green;">'
            MsgBox(0, '', 'after changing')
        Else
            MsgBox(0, '', 'about to change')
            $OneImage.setAttribute("style") = "border:5px solid green;"
            MsgBox(0, '', 'after changing')
        EndIf
        ExitLoop
    EndIf
Next
Edited by dragan
Link to comment
Share on other sites

Hello dragan
thanks for your interesting script.
from tests I have done with your scrip it is found that:

1) when I run the script on win7 - IE10, it works well and the instruction that is executed is the one with "outerHTML"

$OneImage.outerhtml = StringTrimRight($OneImage.outerhtml, 1) & ' style="border:5px solid green;">' ; OK on Win7/IE10

however, it also works if I force the execution of setAttribute

$OneImage.setAttribute("style") = "border:5px solid green;" ; also OK on Win7/IE10

------------------------

2) if I run the script on winXP - ie8 then the statement that is executed is the one with "setAttribute", but it fails and does not draw the border

$OneImage.setAttribute("style") = "border:5px solid green;" ; this Fails on WinXP/IE8

However, if I force the execution of "outerHTML" workaround on winXP then it works

$OneImage.outerhtml = StringTrimRight($OneImage.outerhtml, 1) & ' style="border:5px solid green;">' ; OK on WinXP/IE8

thanks again dragan for your tip, and the workaround.

well, now my best goal would be to be able to run the "$OneImage.setAttribute" statement on WinXP/IE8, because it should be the best way to turn on and turn off often and quickly that border.

I also hope to find the final solution because I would like to understand if it is possible or not in some way be able to use SetAttribute on IE8

any solution is always welcome
 

thanks

Edited by Pincopanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • Solution

Well, this is a WILD guess, I've no idea if this will work on IE8, but it seems that style is ALSO an object, so you use "SetAttribute" function to "style":

#include <IE.au3>
$oIE = _IECreate()
Local $sHTML = '<html><body><img src="http://aut1.autoit-cdn.com/site/wp-content/themes/TheCorporation/images/logo@2x.png" width="210"height="72" alt="AutoItScript" id="logo" /></body></html>'
_IEBodyWriteHTML($oIE, $sHTML)
Local $AllImages = _IETagNameGetCollection($oIE, 'img')
For $OneImage in $AllImages
    If $OneImage.id = 'logo' Then
        MsgBox(0, '', 'about to change')
        $OneImage.style.setAttribute('border', '5px solid green')
        MsgBox(0, '', 'after changing')
        ExitLoop
    EndIf
Next
Edited by dragan
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

×
×
  • Create New...