For years I've used the code shown below to log into a website. It 1) opens Chrome, 2) allows me to manually step through the robot test page and 3) inserts my username. Today the macro is no longer able to insert my username. It throws the following error:
Run-time error '7':
NoSuchElementError
Element not found for Id=user
The relevant source code for the username input box is
<input class="evo-input-text__input" type="email" name="user" id="user" style="text-transform:none">
so the element "id" is still "user".
Why isn't the macro still able to insert my username and what code will insert my username?
Here is the macro:
Sub Login_Selenium()
'
' Note: added reference to Selenium Type Library
'
Dim WDriver As New WebDriver
' Open Chrome and navigate to login page
WDriver.Start "Chrome", ""
WDriver.Get "https://www.prudential.com/login/"
' Maximize the window
WDriver.Window.Maximize
Application.Wait (Now + TimeValue("0:00:02"))
' On 03/04/23 the website now uses a "I'm not a robot" page. A msgbox is now used to allow the macro to proceed after you have manually completed the robot test.
qq = MsgBox("Click through the ""I'm not a robot"" page and then click ""OK"" to proceed.")
' Enter the username -- this is the line that fails
WDriver.FindElementById("user").SendKeys ("abcdefg")
'
' do other stuff
'
End sub
If you look at the page source that is returned by the driver, you can see that the page code contains a lot of Java script code. I wrote the complete code that was returned by the driver into a file for further inspection:
WDriver.Get "https://www.prudential.com/login/"
Application.Wait (Now + TimeValue("0:00:05"))
WriteTextfile WDriver.PageSource, Environ("Temp") & "\" & "SO78124249.txt"
using this piece of code
Sub WriteTextfile(txt, fileName As String)
Dim file As Integer
file = FreeFile
If Dir(fileName) <> "" Then Kill fileName
Open fileName For Output As #file
Write #file, txt
Close #file
End Sub
Now looking for the input form, I found this:
<form class="login" method="POST" novalidate="" autocomplete="off"
id="evoLoginForm" data-qa="evoLoginForm"
action="https://www.prudential.com/api/public/platform/login/v1/ssologin">
(...)
<evo-input-text class="login__input input-username"
input-name="user" input-type="email" a11y-aria-required="true"
hide-asterisk=" type="box-label-outside"
label="Username" error-messages=" styleurl="/etc.clientlibs/pru/clientlibs/clientlib-dependencies.min.css">
</evo-input-text>
I am not a real HTML expert, but I assume that this evo-input-text
is a custom tag and the Javascript code deals with it. What's interesting is that this element doesn't have an ID set. As your code uses this page source, it cannot find the ID.
I tried to locate the element by classname instead ("input-username"), and that worked:
Dim inputBox As WebElement
Set inputBox = WDriver.FindElementByClass("input-username")
inputBox.SendKeys "abcdefg"
Update Setting the password worked for me (only) by setting the focus first. I am not sure if there is any "Setfocus" method, but using Click
did the trick:
Dim passwordBox As WebElement
Set passwordBox = WDriver.FindElementByClass("input-password")
passwordBox.Click
passwordBox.SendKeys "topsecret"