Sign in to follow this  
Followers 0
dushkin

IE Click link

8 posts in this topic

Hi all,

 I decided to learn automating internet explorer.

 For that I started playing with the IE APIs.

 I decided to try and automate amazon for example.

 I had some successes, but for some reason, I fail to click the "Ages 6-8" link!

 I don't understand what is wrong!!!

 Here is the code:

$sSiteLink = "http://www.amazon.com/"

$oIE = _IECreate ($sSiteLink)

_IELoadWait($oIE)

$oForm = _IEFormGetObjByName($oIE, "site-search")
$oField = _IEFormElementGetObjByName($oForm, "field-keywords")
_IEFormElementSetValue($oField, "children's books")
_IEFormSubmit($oForm)

_IELoadWait($oIE)

_IELinkClickByText($oIE, "Children's eBooks")

_IELoadWait($oIE)

Local $oLinks = _IELinkGetCollection($oIE)
For $oLink In $oLinks
    $sTxt = _IEPropertyGet($oLink, "innerText")
    If StringInStr($sTxt, "Ages 6-8") Then
    ;If StringInStr($sTxt, "Animals") Then
        $inner = _IEPropertyGet($oLink, "innertext")
        ConsoleWrite($inner & @CRLF)
        $d = _IEAction($oLink, "click")
        ConsoleWrite($d & @CRLF)
        _IELoadWait($oIE)
        ExitLoop
    EndIf
Next

This is the output:

--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
Ages 6-8 
1
+>21:42:00 AutoIt3.exe ended.rc:0
>Exit code: 0    Time: 11.633
 
I also tried to use _IELinkClickByText($oIE, "Ages 6-8") but this one gives me a no_match exception.
 
When I printed all the links innertexts, I got the title of the link along with the current counting of the books. Something like Ages 6-8 21351
But I use StringInStr, and also _IEAction returns 1. Also _IELinkClickByText is supposed to support sub texts.
 
Please advice.
 
Thanks!

Share this post


Link to post
Share on other sites



What version of Autoit and IE? FWIW, your code works as expected for me when running under the latest version of Autoit, which was released a few days ago.

Share this post


Link to post
Share on other sites

3.3.10.0 and IE 11.0.9600.16476

Share this post


Link to post
Share on other sites

Solved the problem. For some reason I put amazon.com in compatability view mode. When I removed it - it worked!

Thanks !

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi All, I continue with my IE and Amazon experience.

This time I stambled on a very wired phenomenon.

Look at the following code:

#include <IE.au3>
#include <Date.au3>
#include <MsgBoxConstants.au3>

;------------------------
; Verify www.amazon.com is on the compatability list of IE11
;------------------------

$sSiteLink = "http://www.amazon.com/"

$oIE = _IECreate($sSiteLink)

Sleep(3000)

WinSetState("Amazon", "", @SW_MAXIMIZE)
WinActivate("Amazon", "")
WinWaitActive("Amazon", "")

Sleep(2000)

MouseMove(70, 450)
MouseClick("left", 70, 450) ;books

Sleep(1000)

MouseMove(310, 290)
MouseClick("left", 310, 290) ;kindle books
_IELoadWait($oIE)

Sleep(1000)

_IELinkClickByText($oIE, "Children's Books")
Sleep(1000)

$olinks = _IELinkGetCollection($oIE) ; 0-based index

$bSecondAge68Link = False

For $olink In $olinks
  $sText = $olink.innerText
  If StringInStr($sText, "Ages 6-8") Then
      If $bSecondAge68Link == True Then
          _IEAction($olink, "Click")
          _IELoadWait($oIE)
          Sleep(4000)
          ExitLoop
      EndIf
      $bSecondAge68Link = True
  EndIf
Next

_IELoadWait($oIE)
Sleep(1000)

;Iterate pages
While 1

  $bFoundBook = False

  $sLocationURL = _IEPropertyGet($oIE, "locationurl")

  $count = StringInStr($sLocationURL, "&page=5&") ; 70 good
  if($count > 0) Then
     ConsoleWrite($sLocationURL & @CRLF)
   EndIf

  $olinks = _IELinkGetCollection($oIE) ; 0-based index

  ;Iterate links on page
  For $olink In $olinks
      $sText = $olink.innerText


      If $count > 0 And StringLen($sText) >= 10 Then 
         ConsoleWrite($sText & @CRLF)
      EndIf
   Next

   If $count > 0 Then
      Exit
   EndIf

    Local $oNextPage = _IEGetObjById($oIE, "pagnNextString")
    _IEAction($oNextPage, "Click")
    _IELoadWait($oIE)
WEnd

Here, I go to the category of age 6-8 and start paging in it.

I am defining a page to stop on, and then I print to console the inner texts of all the links on the page.

The phenomenon is that only part of them is printed, and I don;t understand why.

For example:

The books list in the page: only the first three is printed. the rest is not! Why??

Note that the 5 on 

      $count = StringInStr($sLocationURL, "&page=5&") ;

is an arbitrary value.

 

In addition, you probably noticed that I use my mouse for the first 2 clicks. This is becuase I am not sure how to click them with _IEAction. Maybe you have an advice for me on that one as well.

Thanks a lot!

Edited by dushkin

Share this post


Link to post
Share on other sites

Hi All, I continue with my IE and Amazon experience.

This time I stambled on a very wired phenomenon.

Look at the following code:

#include <IE.au3>
#include <Date.au3>
#include <MsgBoxConstants.au3>

;------------------------
; Verify www.amazon.com is on the compatability list of IE11
;------------------------

$sSiteLink = "http://www.amazon.com/"

$oIE = _IECreate($sSiteLink)

Sleep(3000)

WinSetState("Amazon", "", @SW_MAXIMIZE)
WinActivate("Amazon", "")
WinWaitActive("Amazon", "")

Sleep(2000)

MouseMove(70, 450)
MouseClick("left", 70, 450) ;books

Sleep(1000)

MouseMove(310, 290)
MouseClick("left", 310, 290) ;kindle books
_IELoadWait($oIE)

Sleep(1000)

_IELinkClickByText($oIE, "Children's Books")
Sleep(1000)

$olinks = _IELinkGetCollection($oIE) ; 0-based index

$bSecondAge68Link = False

For $olink In $olinks
  $sText = $olink.innerText
  If StringInStr($sText, "Ages 6-8") Then
      If $bSecondAge68Link == True Then
          _IEAction($olink, "Click")
          _IELoadWait($oIE)
          Sleep(4000)
          ExitLoop
      EndIf
      $bSecondAge68Link = True
  EndIf
Next

_IELoadWait($oIE)
Sleep(1000)

;Iterate pages
While 1

  $bFoundBook = False

  $sLocationURL = _IEPropertyGet($oIE, "locationurl")

  $count = StringInStr($sLocationURL, "&page=5&") ; 70 good
  if($count > 0) Then
     ConsoleWrite($sLocationURL & @CRLF)
   EndIf

  $olinks = _IELinkGetCollection($oIE) ; 0-based index

  ;Iterate links on page
  For $olink In $olinks
      $sText = $olink.innerText


      If $count > 0 And StringLen($sText) >= 10 Then 
         ConsoleWrite($sText & @CRLF)
      EndIf
   Next

   If $count > 0 Then
      Exit
   EndIf

    Local $oNextPage = _IEGetObjById($oIE, "pagnNextString")
    _IEAction($oNextPage, "Click")
    _IELoadWait($oIE)
WEnd

Here, I go to the category of age 6-8 and start paging in it.

I am defining a page to stop on, and then I print to console the inner texts of all the links on the page.

The phenomenon is that only part of them is printed, and I don;t understand why.

For example:

The books list in the page: only the first three is printed. the rest is not! Why??

Note that the 5 on 

      $count = StringInStr($sLocationURL, "&page=5&") ;

is an arbitrary value.

 

In addition, you probably noticed that I use my mouse for the first 2 clicks. This is becuase I am not sure how to click them with _IEAction. Maybe you have an advice for me on that one as well.

Thanks a lot!

 

I don't understand your first problem, but as far as navigating to that link you can use LinkClickByText which you pass the text shown on the page for the link to.

#include <IE.au3>
$oAmazon = _IECreate ("http://www.amazon.com") or $oAmazon = _IECreate ("http://www.amazon.com/Kindle-eBooks/b?ie=UTF8&node=154606011")
_IELinkClickByText($oAmazon, "Kindle Books")

better yet, why not just go straight there from the getgo on your IECreate?

Share this post


Link to post
Share on other sites

maybe the "very wired phenomenon" is due to the fact that the link collection is not still complete.
try to change

Local $oLinks = _IELinkGetCollection($oIE)

with

Do ; wait for the link collection
    Local $oLinks = _IELinkGetCollection($oIE)
Until IsObj($oLinks) ; when $oLinks IsObj then the collection is ready

also,
sometime _IELoadWait($oIE) don't wait correctly that the page is complete and so the script go ahead also if the page is not completly loaded.

To test, try to insert a sleep() of few more seconds instead of  the _IELoadWait($oIE)


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

Thanks guys for your help. I will check it.

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