0
votes

I'm using a VBA macro in Excel 2016 along with Chrome and Selenium WebDriver to log-in to a website. With VBA and IE I found a way to log in using the old SendKeys method

' Open the "AARP" web page
Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = True
    .Navigate "https://www.medicare.uhc.com/AARP"
    .Top = 0
    .Left = 0
    .Height = 1056
    .Width = 1936
End With
' Loop until the page is fully loaded
Do Until IE.ReadyState = 4 And Not IE.Busy
    DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:04"))

' Click the "Sign In" button
IE.Document.getElementsByClassName("btn btn-primary btn-block")(0).Click

' Loop until the page is fully loaded
Do Until IE.ReadyState = 4 And Not IE.Busy
    DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:12"))

Set WSS = CreateObject("WScript.Shell")
my_PW = "123"
my_ID = "abc"

IE.Document.getElementById("EMAIL").Focus
Application.Wait (Now + TimeValue("0:00:02"))
WSS.SendKeys my_ID
Application.Wait (Now + TimeValue("0:00:02"))

IE.Document.getElementById("PASSWORD").Focus
Application.Wait (Now + TimeValue("0:00:02"))
WSS.SendKeys my_PW
Application.Wait (Now + TimeValue("0:00:02"))

IE.Document.getElementById("submitBtn").Click

Using more traditional code such as

IE.Document.getElementById("EMAIL").Value = "abc"
IE.Document.getElementById("PASSWORD").Value = "123"

failed. You need to physically interact with the textbox.

I'm trying to migrate the code to work with Chrome and Selenium WebDriver. When I use code such as

WDriver.FindElementById("EMAIL").SendKeys ("")     ' focus
WDriver.FindElementById("EMAIL").SendKeys ("abc")

WDriver.FindElementById("PASSWORD").SendKeys ("")  ' focus
WDriver.FindElementById("PASSWORD").SendKeys ("123")

it puts the text in the proper boxes, but when I click the "Submit" button the website doesn't log me in. I presume that the code fails because it is not physically interacting with the text boxes. How can I physically interact with the text boxes (like the old SendKeys method did with IE) using Selenium WebDriver in VBA?

Thanks for your help...Ron

1

1 Answers

0
votes

With ie you need to trigger the change event listeners associated with those input fields. I can't test with selenium vba but I had no problem with Python selenium provided I added a wait condition.

Option Explicit

Public Sub Login()
    Dim ie As SHDocVw.InternetExplorer

    Set ie = New SHDocVw.InternetExplorer

    With ie
        .Visible = True
        .navigate2 "https://www.medicare.uhc.com/AARP"

        While .Busy Or .readyState <> 4: DoEvents: Wend

        .document.querySelector(".btn-primary").Click

        While .Busy Or .readyState <> 4: DoEvents: Wend
        
        Dim t As Date, ele As Object
        t = Timer
        Do
            DoEvents
            On Error Resume Next
            Set ele = .document.querySelector("#EMAIL")
            On Error GoTo 0
            If Timer - t = 10 Then Exit Do       '<==To avoid infinite loop
        Loop While ele Is Nothing
        
        Dim evt As Object
        Set evt = .document.createEvent("HTMLEvents")
        evt.initEvent "change", True, False
        
        If Not ele Is Nothing Then
        
            With .document.querySelector("#EMAIL")
                .Value = "abc"
                .dispatchEvent evt
            End With
            
            With .document.querySelector("#PASSWORD")
                .Value = "abc"
                .dispatchEvent evt
            End With
            
            .document.querySelector("#submitBtn").Click
        
        End If
        
        Stop

        .Quit
    End With

End Sub

Python selenium:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = 'https://www.medicare.uhc.com/AARP'
d = webdriver.Chrome()
d.get(url)
d.find_element_by_css_selector('.btn-primary').click()
WebDriverWait(d,5).until(EC.presence_of_element_located((By.ID, "EMAIL"))).send_keys('abc')
d.find_element_by_id('PASSWORD').send_keys('abc')
d.find_element_by_id('submitBtn').click()