3

My ultimate goal is to automate using VBA to open a browser, and input username and password for a website, and then click login to further navigate to different pages and download files!

I want to begin with the simplest trial, however, of doing a Google search! I think the idea would be similar because both require to input something and click something.

I found in IE -> Tools(Alt+X) -> F12 Developer Tools can show html codes of website, and even more convenient, it seems I can select regions and get keywords that I'm interested in! Here are 2 screenshots of those regions, the first one is for the search bar, I found "input name = "q", and the second one is for the search button, where I found "input name = "btnK".

And here are my codes:

Sub test()

Set IE = CreateObject("InternetExplorer.Application")
my_url = "https://www.google.ca/?gws_rd=ssl"
IE.Visible = True
IE.navigate my_url

'For example, if I want to search "123"
IE.Document.getElementById("q").Value = "123"

'Click the "Search" button
IE.Document.getElementById("btnK").Click


End Sub

It returns error at IE.Document.getElementById("q").Value = "123",

Method 'Document' of object 'iwebbrowser2' failed.

Don't understand where has gone wrong. I'm really new to how to use VBA to control browsers... Please help, thank you all!

2 Answers 2

1

Ensure you use the right wait syntax so page has loaded. You also don't need to loop to submit the form. Just submit direct with

.forms(0).submit

Code:

Option Explicit
Public Sub test()
    Const MY_URL = "https://www.google.ca/?gws_rd=ssl"
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate MY_URL
        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .getElementById("lst-ib").Value = "123"
            .forms(0).submit
        End With
        'Other code
        '.Quit  '<== Uncomment me later
    End With
End Sub
Sign up to request clarification or add additional context in comments.

7 Comments

By adding a "While .Busy Or .readyState < 4: DoEvents: Wend", 123 was input in the search bar! What does this statement do? But seems the click did not work...
The statement ensures that the page has loaded (except in cases of javascript heavy pages.)
What is "readyState", and what value it can take? And what is "document.forms(0)"? So I tried with another website, the statement will stop at "While .Busy Or .readyState < 4: DoEvents: Wend", probably that's the javascript heavy pages you've mentioned? How to possibly improve this? Thank you!
Is this question answered? readystate can take values 0-4. You are entering data into a form which you then submit by pressing the search button. The above targets the form itself and submits it direct.
Thanks, but what does the parameter "0" mean? And how do you specify which button to press if multiple button showing on the same page?
|
1

The name of an HTML element is not the same as the ID.

For example, "q" is not the ID of the search bar but it's name. It's ID is "lst-ib"

Adding code:

Sub test()

    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    my_url = "https://www.google.ca/?gws_rd=ssl"
    ie.Visible = True
    ie.navigate my_url

    Do While ie.Busy
        DoEvents
    Loop
    Wait 3

    'For example, if I want to search "123"
    ie.Document.getElementById("lst-ib").Value = "123"

    ' Pressing enter
    SendKeys "{ENTER}"

End Sub

Sub Wait(seconds As Integer)

    Application.Wait Now + timeValue("00:00:0" & CStr(seconds))

End Sub

6 Comments

Thanks, but I tried to replace "q" with "lst-ib" just now, but still not working, and returned the same error message... And there is another statement ".getElementsByName", with "q" the result is still the same...
@Passer-by added code, not sure what the problem your experiencing was, a lot of times you need to wait for IE to finish processing a task
So the "Do While ie.Busy" a statement to wait for IE? And I also see quite a lot in addition to .Busy, people use .readyState, not sure what do these statements exactly do, but appears to wait for IE. What is the purpose to add another "Wait" program you've created right after the loop? Thanks!
@Passer-by yes the while .busy statement just makes the program wait until internet explorer has loaded a page but sometimes when there’s heavy javascript I need to make my program wait longer for safety measures thats why I use that Wait function I created
Thanks, and it seems the command SendKeys "{ENTER}" does not specify which button, on the Google search it's simply that only has one search button, but what if for example there are "Next" and "Previous" buttons, and possible more other button on the same page, how would I refer to a button that I want to click? Much appreciated!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.