Jump to content
Sign in to follow this  
Chimp

how to change an object style in a web page?

Recommended Posts

Chimp

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

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

Share this post


Link to post
Share on other sites
abberration

I think your problem lies in the fact that you are only viewing an image on a blank HTML page. Look at the source code of the page and it is:

<HTML></HTML>

Therefore, _IEImgGetCollection has no data to collect. I think you need to try your code with an actual HTML page.


RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites
dragan

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
  • Like 1

Share this post


Link to post
Share on other sites
Chimp

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

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

Share this post


Link to post
Share on other sites
dragan

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

Share this post


Link to post
Share on other sites
Chimp

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

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

Share this post


Link to post
Share on other sites
dragan

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
  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
Chimp

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

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

Share this post


Link to post
Share on other sites
dragan

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
  • Like 1

Share this post


Link to post
Share on other sites
Chimp

well,  your wild guess was right! :)
now it work fine also on WinXP/IE8.

Nice job dragan! :thumbsup:
Thanks again
bye


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

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  

  • Similar Content

    • RamaKrishna9
      By RamaKrishna9
      What are the minimum system requirements(like Windows OS version, IE browser version, etc,.) for IE.au3(3.3.14.2) to work properly?
    • MakzNovice
      By MakzNovice
      Hello Experts,
      I am working on modifying table data of a browser currently in IE (soon in FF)
      My sequence of operations:
      1. Activate Browser
      2. Get Browser table
      3. Modify data  in table
      4. Suppress any warning/error message from webpage
      5. Activate the Webpage again 
      6. Send {F11} key sequence twice to achieve some update without Updating form ( {F5})
       
      My problem is at Step 5, as it activates the wrong window ( last used application rather than my browser window)
      My code snippet for it as follows
      $bFoundWindow = False Local $aList = WinList("[REGEXPTITLE:(?i)(.*Internet Explorer.*)]") Local $hWND = 0 For $i = 1 To $aList[0][0] If False = $bFoundWindow And $aList[$i][0] <> "" And BitAND(WinGetState($aList[$i][1]), 1) Then WinActivate($aList[$i][1]) ; 1. Activate Browser window WinSetState ( $aList[$i][1], "", @SW_MAXIMIZE) $hWND = $aList[$i][1] $objEditIE = _IEAttach ($aList[$i][1], "hwnd") $oTitles = _IETagNameGetCollection($objEditIE, "TITLE") For $oTitle In $oTitles $sTitle = $oTitle.innerText If $sTitle = $searchString Then MsgBox(0,"APPLICATION Found", "Got Edit List Window", 1) $bFoundWindow = True ExitLoop EndIf Next EndIf Next If False = $bFoundWindow Then MsgBox(0,"Error","Please launch Edit window for updating values in Browser prior to script execution",10) Exit EndIf ; MY CODE HERE TO MODIFY TABLE ; 2. Get Browser table ; 3. Modify data  in table ; Check if message from webpage pop-up window exists if so click ok on it Local $nCounter = 0 Local $nMaxCounter = 5 while 1 If WinExists("Message from webpage","") Then ControlClick("Message from webpage","","Button1","primary",1) ;4. Suppress any warning/error message from webpage Else $nCounter = $nCounter + 1 Sleep(100) ;MsgBox(0,"Counter", $nCounter, 1) If $nCounter = $nMaxCounter Then ExitLoop EndIf WEnd ;5. Activate the Webpage again  WinActivate($hWND) ; have already tried WinActivate($aList[$i][1]) but no success ;6. Send {F11} key sequence twice to achieve some update without Updating form ( {F5}) Send("{F11}") WinActivate($hWND) ;WinActivate($aList[$i][1]) Sleep(100) Send("{F11}") Any support will be highly appreciated
    • MakzNovice
      By MakzNovice
      Hello Experts,
      I am working on custom application details of which I am unable to share.
      But here is statement of my problem, I have two IE windows one window with Title "Parent " and other with "TableEdit", but for some reason when I get WinList they are shown as 2 processes each. ( see attached image)
      Since my most important motive is to modify table data in 'TableEdit' I try to access the table in it
      My code snippet is 
      ***********************************************************************************************
      $searchString = "TableEdit"
      Local $aList = WinList("[REGEXPTITLE:(?i)(.*Internet Explorer.*)]")
      _ArrayDisplay($aList)
      For $i = 1 To $aList[0][0]
          If $aList[$i][0] <> "" And BitAND(WinGetState($aList[$i][1]), 2) Then
              ;MsgBox($MB_SYSTEMMODAL, "", "Title: " & $aList[$i][0] & @CRLF & "Handle: " & $aList[$i][1])
              If 0 <> StringInStr($aList[$i][0],"TableEdit") Then
                  $objEditIE = _IEAttach ($aList[$i][1])
                  ExitLoop
              EndIf
          EndIf
      Next
      WinActivate($objEditIE)
      $oPageContentListFrame = _IEFrameGetObjByName($objEditIE, "iframe_1")
      $iNumFrames = @extended
      MsgBox(0,"Got pagecontent",@error) ; returns 3, indicating invalid data type
      MsgBox(0,"Number of Frame",$iNumFrames) ; returns 1, expected value is 1 
      ***********************************************************************************************
      and If I do code as below the return value is different
      $searchString = "TableEdit"
       $objEditIE = _IEAttach ($searchString, "title")
      WinActivate($objEditIE)
      $oPageContentListFrame = _IEFrameGetObjByName($objEditIE, "iframe_1")
      $iNumFrames = @extended
      MsgBox(0,"Got pagecontent",@error) ; returns 0
      MsgBox(0,"Number of Frame",$iNumFrames) ; returns 0. expected value is 1
      ***********************************************************************************************
      What is wrong with my code? 
      Why do I see two process each for IE for each window?
       
      WinListImage.bmp
    • Tippex
      By Tippex
      I have a problem trying to use _IEAttach to recover control after a Windows Explorer reset (e.g. on Windows 7), since all the 
      InternetExplorer.Application objects appear to be destroyed by this event. An example of this is when a PC has some kind of fault causing a pop-up error message "Windows Explorer has stopped working". I can simulate this event by using Windows Task Manager to end the Explorer.exe process then to file, new task (run...) Explorer.exe
      After this event, _IEAttach gives an @error of: 
      7 ($_IEStatus_NoMatch) - No Match
      However, using WinList() followed by WinGetText() I can recover the URL for an IE session that has lost its 
      InternetExplorer.Application object which could be used to _IECreate a new session (the $iTryAttach fails) and WinKill() the original session but the result is a if a refresh had been done (any form entries are lost) . Unfortunately form entries do not appear as text or hidden text to Windows so I'm looking for the best way to read the contents of an Internet Explorer screen (even if just text without structure). 
      When there is no InternetExplorer.Application object (because of some Windows fault) but still with an Internet Explorer session running, I tried to read its contents by a "Select All" & "Copy to Clipboard" but its form entries are blanked and combo boxes choices are all displayed with no way of finding out which ones were selected.
      Local $ClipBoardSave = _ClipBoard_GetData() ;not ClipGet() which is text only ClipPut("") ;Clear User's Clipboard Local $sText = "" Local $hWnd = WinActivate("Window Title", "") If IsHWnd($hWnd) Then Local $bStatusCtrlSend = ControlSend("Window Title", "", "", "^a^c") If $bStatusCtrlSend Then Local $bStatusCtrlClick = ControlClick("Window Title", "", "") If $bStatusCtrlClick Then $sText = ClipGet() _ClipBoard_SetData($ClipBoardSave) ;not ClipPut($ClipBoardSave) which is text only  
      Is  there a solution to this please?
    • Tippex
      By Tippex
      I'm having a problem with _IEBodyReadText() which is a function from the IE.au3 UDF include.
      My problem is that randomly it will crash my script with:
      Return SetError($_IESTATUS_Success, 0, $oObject.document.body.innerText) Return SetError($_IESTATUS_Success, 0, $oObject.document.body^ ERROR I don't know why the error isn't caught by __IEErrorHandlerRegister but looking at this function, I can see that there is a check done by IsObj($oObject) to ensure that the object "$oObject" exists but no check that it contains the property "innerText"
      ; #FUNCTION# ==================================================================================================================== ; Author ........: Dale Hohm ; =============================================================================================================================== Func _IEBodyReadText(ByRef $oObject) If Not IsObj($oObject) Then __IEConsoleWriteError("Error", "_IEBodyReadText", "$_IESTATUS_InvalidDataType") Return SetError($_IESTATUS_InvalidDataType, 1, 0) EndIf If Not __IEIsObjType($oObject, "browserdom") Then __IEConsoleWriteError("Error", "_IEBodyReadText", "$_IESTATUS_InvalidObjectType", "Expected document element") Return SetError($_IESTATUS_InvalidObjectType, 1, 0) EndIf ; Return SetError($_IESTATUS_Success, 0, $oObject.document.body.innerText) EndFunc ;==>_IEBodyReadText I'm struggling to find out how to test if "$oObject.document.body.innerText" exists before allowing the following Return statement to reference it with the assumption that it does exist (crashing my script when it doesn't).
      Any ideas please?
×