Jump to content

IEAction ( click) not working 100% of the Time. Any advice to get it working correctly?


Recommended Posts

Guys,

        Can you guys tell me why this won't work correctly? It does click through but only about 50-60% of the time. The other times, it just gets stuck in the main loop and for some reason and it seems the script is running slower than normal. Any advice to speed it up?

#include <array.au3>
#include <IE.au3>
ConsoleWrite('!--------- STARTING ----------' & @CRLF)
Local $oIE = _IECreate("http://www.beamantoyota.com/new-inventory/index.htm")

While 1

        Local $sHTML = _IEDocReadHTML($oIE)
        Local $oListItems = _IETagNameGetCollection ($oIE, "LI")
        For $oListItem In $oListItems
            Local $odivs = _IETagNameGetCollection($oListItem, 'div')
            For $odiv in $odivs
                Local $dataVin = $odiv.getAttribute("data-vin")
                If $dataVin <> '' Then
                    ConsoleWrite('+ Found it, data-vin: ' & $dataVin & @CRLF)
                    Local $oAnchors = _IETagNameGetCollection($odiv, 'a')
                    For $oAnchor in $oAnchors
                        Local $sHref = $oAnchor.href
                        ConsoleWrite(">    Link Info: " &$sHref& @CRLF)
                        ExitLoop
                    Next

                    Local $imgs = _IETagNameGetCollection($odiv, 'img')
                    For $img In $imgs
                        Local $pic = $img.src
                        ConsoleWrite(">    Image Info: " &$pic& @CRLF)
                    Next

                    Local $titles = _IETagNameGetCollection($oListItem, 'a')
                    For $title In $titles
                        Local $datatitle = $title.getAttribute("data-title")
                        If $datatitle <> '' Then
                        ConsoleWrite('> Found it, data-title: ' & $datatitle & @CRLF)
                        ExitLoop
                        EndIf
                    Next

                    ConsoleWrite('----------------------------' & @CRLF)


                EndIf



            Next


        Next
    $aPage = StringRegExp($sHTML, "Page (\d*) of (\d*)", 1)
    ;_ArrayDisplay($aPage)
    If $aPage[0] = $aPage[1] Then ExitLoop

    Local $oLinks = _IELinkGetCollection($oIE)
    $sMyString = "Next"
        For $oLink In $oLinks
            Local $sLinkText = _IEPropertyGet($oLink, "innerText")
            If StringInStr($sLinkText, $sMyString) Then
                _IEAction($oLink, "click")
                _IELoadWait($oIE)
                ExitLoop
            EndIf
        Next

WEnd
ConsoleWrite('!--------- ENDING ----------' & @CRLF)
_IEQuit($oIE)
Link to comment
Share on other sites

What is it doing when it "gets stuck"?

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

The reason why is this happening is because of the "pop-up" window on your webpage.

There are two solutions to this problem:

1. find a way to close that pop-up before clicking the link

#include <array.au3>
#include <IE.au3>
ConsoleWrite('!--------- STARTING ----------' & @CRLF)
Local $oIE = _IECreate("http://www.beamantoyota.com/new-inventory/index.htm")

While 1
    _IELoadWait($oIE)

    Local $sHTML = _IEDocReadHTML($oIE)
    Local $oListItems = _IETagNameGetCollection ($oIE, "LI")
    For $oListItem In $oListItems
        Local $odivs = _IETagNameGetCollection($oListItem, 'div')
        For $odiv in $odivs
            Local $dataVin = $odiv.getAttribute("data-vin")
            If $dataVin <> '' Then
                ConsoleWrite('+ Found it, data-vin: ' & $dataVin & @CRLF)
                Local $oAnchors = _IETagNameGetCollection($odiv, 'a')
                For $oAnchor in $oAnchors
                    Local $sHref = $oAnchor.href
                    ConsoleWrite(">    Link Info: " &$sHref& @CRLF)
                    ExitLoop
                Next

                Local $imgs = _IETagNameGetCollection($odiv, 'img')
                For $img In $imgs
                    Local $pic = $img.src
                    ConsoleWrite(">    Image Info: " &$pic& @CRLF)
                Next

                Local $titles = _IETagNameGetCollection($oListItem, 'a')
                For $title In $titles
                    Local $datatitle = $title.getAttribute("data-title")
                    If $datatitle <> '' Then
                    ConsoleWrite('> Found it, data-title: ' & $datatitle & @CRLF)
                    ExitLoop
                    EndIf
                Next
                ConsoleWrite('----------------------------' & @CRLF)
            EndIf
        Next
    Next
    $aPage = StringRegExp($sHTML, "Page (\d*) of (\d*)", 1)
    ;_ArrayDisplay($aPage)
    If $aPage[0] = $aPage[1] Then ExitLoop

    Local $oLinks = _IETagNameGetCollection($oIE, 'a')
    For $oLink in $oLinks
        If $oLink.getAttribute("rel") = "next" Then
            _ClosePopUpIfExist()
            _IEAction($oLink, 'click')
            ExitLoop
        EndIf
    Next

WEnd
ConsoleWrite('!--------- ENDING ----------' & @CRLF)
_IEQuit($oIE)

Func _ClosePopUpIfExist()
    Local $oLinks = _IETagNameGetCollection($oIE, 'a')
    For $oLink in $oLinks
        If $oLink.classname == 'ui-dialog-titlebar-close ui-corner-all' Then
            _IEAction($oLink, 'click')
            Return
        EndIf
    Next
EndFunc

2. extract the URL, and navigate to the URL of the link you want to open:

#include <array.au3>
#include <IE.au3>
ConsoleWrite('!--------- STARTING ----------' & @CRLF)
Local $oIE = _IECreate("http://www.beamantoyota.com/new-inventory/index.htm")

While 1
    _IELoadWait($oIE)

    Local $sHTML = _IEDocReadHTML($oIE)
    Local $oListItems = _IETagNameGetCollection ($oIE, "LI")
    For $oListItem In $oListItems
        Local $odivs = _IETagNameGetCollection($oListItem, 'div')
        For $odiv in $odivs
            Local $dataVin = $odiv.getAttribute("data-vin")
            If $dataVin <> '' Then
                ConsoleWrite('+ Found it, data-vin: ' & $dataVin & @CRLF)
                Local $oAnchors = _IETagNameGetCollection($odiv, 'a')
                For $oAnchor in $oAnchors
                    Local $sHref = $oAnchor.href
                    ConsoleWrite(">    Link Info: " &$sHref& @CRLF)
                    ExitLoop
                Next

                Local $imgs = _IETagNameGetCollection($odiv, 'img')
                For $img In $imgs
                    Local $pic = $img.src
                    ConsoleWrite(">    Image Info: " &$pic& @CRLF)
                Next

                Local $titles = _IETagNameGetCollection($oListItem, 'a')
                For $title In $titles
                    Local $datatitle = $title.getAttribute("data-title")
                    If $datatitle <> '' Then
                    ConsoleWrite('> Found it, data-title: ' & $datatitle & @CRLF)
                    ExitLoop
                    EndIf
                Next
                ConsoleWrite('----------------------------' & @CRLF)
            EndIf
        Next
    Next
    $aPage = StringRegExp($sHTML, "Page (\d*) of (\d*)", 1)
    ;_ArrayDisplay($aPage)
    If $aPage[0] = $aPage[1] Then ExitLoop

    Local $oLinks = _IETagNameGetCollection($oIE, 'a')
    For $oLink in $oLinks
        If $oLink.getAttribute("rel") = "next" Then
            _IENavigate($oIE, $oLink.href)
            ExitLoop
        EndIf
    Next

WEnd
ConsoleWrite('!--------- ENDING ----------' & @CRLF)
_IEQuit($oIE)
Link to comment
Share on other sites

I got it working correctly using IENavigate. I did change a piece in my original script. I looked at what you wrote and instead of looking to the specific tag, I already had the links. I just needed to tell it to navigate to that link. It seems to work fine for now. I will update if I run into issues. Thank you for your advice and help.

Local $oLinks = _IELinkGetCollection($oIE)
    $sMyString = "Next"
        For $oLink In $oLinks
            Local $sLinkText = _IEPropertyGet($oLink, "innerText")
            If StringInStr($sLinkText, $sMyString) Then
                _IENavigate($oIE, $oLink.href)
                _IELoadWait($oIE)
                ExitLoop
            EndIf
        Next

WEnd
Link to comment
Share on other sites

Some advices:

  • Reason I used _IELoadWait($oIE) at the top of the While 1 loop is because you do NOT have _IELoadWait($oIE) bellow your _IECreate("http://www.beamantoyota.com/new-inventory/index.htm"). And thus I removed _IELoadWait($oIE) after _IENavigate($oIE, $oLink.href), but if you want to stick to your own code, at least consider adding _IELoadWait($oIE) bellow your _IECreate, so that your script can get full _IEDocReadHTML($oIE).
  • The reason I used this code:
    Local $oLinks = _IETagNameGetCollection($oIE, 'a')
    For $oLink in $oLinks
        If $oLink.getAttribute("rel") = "next" Then
            _IENavigate($oIE, $oLink.href)
            ExitLoop
        EndIf
    Next

    instead of your StringInStr is because mine is much more specific, and there's a higher chance that your script hook some other link you don't want, which also contain "next" string in it's innertext (and you don't want that to happen). You can use If $oLink.getAttribute("rel") = "next" Then with _IELinkGetCollection as well, not only _IETagNameGetCollection

  • If you're using some filtering rules on that webpage (year, model, body style, price), then _IENavigate is not a good way, then it's better to simulate "click", because _IENavigate will probably remove all filtering options to their default values (unless filtering rules are stored into cookies). scratch that, I guess, the filtering rules are also stored into href of the "next" link.

Edited by dragan
Link to comment
Share on other sites

Thanks Dragan. I will update and make the changes. Any idea why your changes navigates back to the home page?

Also, I will move IELoadWait. I though IECreate pretty much waited for the page to load before it returned.

Edited by RickB75
Link to comment
Share on other sites

After it reads the first page. Here's the url.  http://www.beamantoyota.com/index.htm

Here's what I used to check

If $aPage[0] = $aPage[1] Then ExitLoop

    Local $oLinks = _IETagNameGetCollection($oIE, 'a')
    For $oLink in $oLinks
        If $oLink.getAttribute("rel") = "next" Then
            MsgBox(0,"Link",$oLink.href)
            _IENavigate($oIE, $oLink.href)
            ExitLoop
        EndIf
    Next

WEnd
Link to comment
Share on other sites

I have tested this, and it completed successfully for me. I was able to go through all 31 pages, and I wasn't navigated to http://www.beamantoyota.com/index.htm.

I'm on Windows7, 64-bit, IE v10.0.9200.16660

One more thing, I forgot that  _IENavigate also has builtin _IELoadWait function, so you can remove all _IELoadWait functions from your script.

Edited by dragan
Link to comment
Share on other sites

I'm running XP. I'm still getting the home page link in my Msg box. This is weird. I'll remove the IELoadWait and use the StringInStr method for now. 

Dragan, I really, really appreciate your help. Msg me if there's anything I do for you. 

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