0

I have a simple code that uses the IE automation to login to a website (e.g., URL1) and then clicking on a link (e.g., URL2) and waiting until the new page gets ready and so on. Here is the code:

'Part 1: Navigating to URL1
IE = CreateObject("InternetExplorer.Application")
IE.visible = True
IE.Navigate(URL1)
Do Until IE.ReadyState = tagREADYSTATE.READYSTATE_COMPLETE
    Application.DoEvents()
Loop
LinkSet1 = IE.document.all'Storing the current page's links only to help asking my question clearer :)



'Part 2: Entering user name and password and submit
IE.Document.All("UserNameElementID").InnerText = MyUserName
IE.Document.All("PasswordElementID").InnerText = MyPassword
IE.Document.All("SubmitElementID").click
Do Until IE.ReadyState = tagREADYSTATE.READYSTATE_COMPLETE
    Application.DoEvents()
Loop



'Part 3: Search through links to detect a special id on the second page (URL2)
LinkFound = False
 Do Until LinkFound  
     LinkSet2 = IE.document.all'Storing the new page's links only to help asking my question clearer :)       
     For Each Link In IE.document.all
         If InStr(Link.id, "MYSecondURL_id") > 0 Then 
             LinkFound = True               
             Exit For
         End If
     Next
 Loop



'Part 4: Send a message to show that the second URL is found
MsgBox("Page loaded completely!")

My problem is that the above code worked finely when I used Windows 7 with IE 10. But when I updated to Windows 10 with IE 11, always LinkSet2 = LinkSet1 and an infinite loop occurs at Part 3. Any help would be appreciated in advance!

  • You should use a [WebBrowser Control](https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/webbrowser-control-windows-forms) instead of `CreateObject("InternetExplorer.Application")`. Note that the [Use of Application.DoEvents()](https://stackoverflow.com/a/5183623/1115360) could be causing problems in the program, see [How to wait until WebBrowser is completely loaded in VB.NET?](https://stackoverflow.com/a/3275562/1115360) for a correct way to do something when the document load has completed. – Andrew Morton Sep 16 '18 at 12:00
  • Thank you Andrew. Honestly, my code finally after loading several pages, clicks a link to download an Excel file. I first used WebBrowser Control for my purpose, but when clicked on the download link, it led to a "Server in Application" error and after about 1 week of struggling I couldn't solve the problem. Hence, I changed my way to use IE instead. It works completely right on Windows 7 and IE 10, but not on Windows 10 with IE 11. – Saeed Sayyadipour Sep 16 '18 at 12:09

2 Answers2

0

This is because you are using Until Link found. Which means that this loop will never end if there is no link. Instead. i suggest you to use only for each loop. And at the end of loop (When loop exited.) check if there is a link found or not. and perform action accordingly.

'Part 3: Search through links to detect a special id on the second page (URL2)
LinkFound = False 
 LinkSet2 = IE.document.all     
 For Each Link In IE.document.all
     If InStr(Link.id, "MYSecondURL_id") > 0 Then 
         LinkFound = True               
         Exit For
     End If
 Next
If LinkFound=True
'Do what you want to do if there is link
else
 'Do when there is no link in the page
End If
Nadir
  • 87
  • 1
  • 13
  • This method doesn't solve my problem, because it always enters the "else" condition. As I explained, LinkSet2 never updates and it's always the same as LinkSet1 (i.e., the links on the first page), although I see in the IE that the second page is loaded completely.. – Saeed Sayyadipour Sep 16 '18 at 12:17
  • Check page manually if it contains something with ID you are searching. – Nadir Sep 16 '18 at 12:24
  • No No, I know that the second page surely contains the link I want; the loop is only to make sure that the new page is loaded (because I want to click on that link to go to a third page!). The IE.ReadyState loop is not enough, because although it says that the page loading is completed but it's not :)) – Saeed Sayyadipour Sep 16 '18 at 12:37
0

try this

Set window.onload = GetRef("WindowLoad")
Function WindowLoad
  Dim oElm 
Set oElm = document.getElementById("MYSecondURL_id")
if oElm Is Nothing then
    MsgBox("element does not exist")
else        
   MsgBox("element does exist")
end if
End Function

Because some of VBscript functions do not work on newer versions of IE. And there is no doubt that IE is worst explorer in the history of internet surfing.

Nadir
  • 87
  • 1
  • 13
  • Thanks dear Nadir. Although this answer didn't solve my problem, but I agree that IE is the worst explorer! Hence, I switched to Mozilla and used Geckofx tool. That solved my problem. Thanks anyway! – Saeed Sayyadipour Sep 25 '18 at 08:36