0

New to navigating with IE. I m trying to go to a site https://www.thetrainline.com/ and tick the Return button by using element getElementsByID("return").FirstChild.Click However all this does is bring up the site. It doesn't tick the return option.

Any ideas?

Public Function GetHTML() As String

    On Error GoTo ErrorEscape

    Dim URL As String
    Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
    Dim HTML As Object
    Dim myElement As Object

    'Define URL
    URL = "https://www.thetrainline.com/"

    IE.navigate URL
    Application.Wait (Now + TimeValue("0:00:03"))
    MsgBox ("IE Loaded")

    Set myElement = IE.getElementsByID("return").FirstChild.Click

    Exit Function

ErrorEscape:
    Set IE = Nothing
    Set HTML = Nothing

End Function

P.S. I want many users to be able to use this without having to download something. Hence, why I am not using Selenium.

Teamothy
  • 2,000
  • 3
  • 16
  • 26
  • Perhaps you are getting an error on the `Set myElement =` line? I would think `Click` isn't returning anything. – Brian M Stafford Oct 22 '19 at 13:11
  • Use a timeout to detect that the site is "loaded" is not the way to do it. The IE has events to tell that a page is really loaded. https://stackoverflow.com/questions/23299134/failproof-wait-for-ie-to-load. And second getElementById is a function which depends on a DOCUMENT. So get first the document before you fire up this. – Thomas Ludewig Oct 22 '19 at 13:27
  • Thanks both, @ThomasLudewig. So if I don't have a document. But I just want to simply click on the box and select it? –  Oct 22 '19 at 13:38
  • Maybee you will ahve a look at the acc. Javascript function ;) If you get a element with the document.getElementById function you can now do a .click(). you has also a typo in IE.getElement>>>s<< var doc: doc = ie.getdocument : var elm : elm=doc.getElementByID("return").FirstChild : elm.click(). – Thomas Ludewig Oct 22 '19 at 13:49
  • There are numerous samples on this site. Take a look [here](https://stackoverflow.com/q/22670379/5162073). – Brian M Stafford Oct 22 '19 at 14:03
  • It is simply ie.document.getElementById("return").click but you need a proper page load wait before and possibly a timed loop for the element to be present. There are examples of both on SO. – QHarr Oct 22 '19 at 20:08

1 Answers1

0

I try to modify your code to click on 'return' radio button.

Modified code:

Sub demo()
 Dim URL As String
    Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
    Dim HTML As Object
    Dim myElement As Object

    'Define URL
    URL = "https://www.thetrainline.com/"
IE.Visible = True
    IE.navigate URL
    Application.Wait (Now + TimeValue("0:00:03"))
    'MsgBox ("IE Loaded")

    IE.document.getElementById("return").Click

ErrorEscape:
    Set IE = Nothing
    Set HTML = Nothing

End Sub

Output:

enter image description here

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19