'Need VBA code snippet for selenium to click on a web page element
I previously used the following code snippet to click on a selector button in a web page that was opened in internet explorer.
Set inputSelector = HTMLDoc.getElementsByClassName("idp") '<== the collection of input tags on the page
For Each MyHTML_Element In inputSelector
If MyHTML_Element.className = "idp" Then
MyHTML_Element.Click: Exit For
End If
Next
I tried to find the same button in the "idp" class with Selenium using
Set inputBoxes = htmlDoc.FindElementByCss("idp").Text
which is from an example I found on-line. This snippet generates a compiler error because .Text is a string and I'm seeking a collection. What should I use in place of FindElementByCss().Text to return a collection?
Public Sub Sample()
Dim pid As Long
Dim pno As Long: pno = 17556
Dim htmlDoc As WebDriver
'Create Selenium htmlDoc, Verify MS Edge Webdriver and continue
Set htmlDoc = CreateObject("Selenium.WebDriver")
pid = StartEdgeDriver(PortNo:=pno)
If pid = 0 Then Exit Sub
'Specify URL, create Selenium html document and open webpage
sLoginURL = "https://empowercentral.ussco.com/"
htmlDoc.StartRemotely "http://localhost:" & pno & "/", "MicrosoftEdge"
htmlDoc.Get sLoginURL
Set inputSelector = htmlDoc.FindElementByCss("idp").Text
For Each MyHTML_Element In inputSelector
If MyHTML_Element.className = "idp" Then
MyHTML_Element.Click: Exit For
End If
Next
htmlDoc.FindElementById("userNameInput").SendKeys "username"
htmlDoc.FindElementById("nextButton").Click
htmlDoc.FindElementById("passwordInput").SendKeys "password"
htmlDoc.FindElementById("submitButton").Click
End Sub
Here is the html from the website that I'm trying to scrape: Sign in with one of these accounts
<input id="hrdSelection" type="hidden">
<div class="idp" tabindex="1" onkeypress="if (event && (event.keyCode == 32 || event.keyCode == 13)) HRD.selection('http://login.essendant.com/adfs/services/trust');" onclick="HRD.selection('http://login.essendant.com/adfs/services/trust'); return false;"><img class="largeIcon float" src="/adfs/portal/images/idp/idp.png?id=3EADD3E829A20DF612C7A77960FF811E66E3EE6BAE2C33C9B20E7478BAC87548" alt="Customer or Supplier"><div class="idpDescription float"><span class="largeTextNoWrap indentNonCollapsible">Customer or Supplier</span></div></div><div class="idp" tabindex="2" onkeypress="if (event && (event.keyCode == 32 || event.keyCode == 13)) HRD.selection('AD AUTHORITY');" onclick="HRD.selection('AD AUTHORITY'); return false;"><img class="largeIcon float" src="/adfs/portal/images/idp/localsts.png?id=77809433F1EA22C491CA1C7A0836B144BB32E92C12110BF2A2D30810B030734D" alt="Active Directory"><div class="idpDescription float"><span class="largeTextNoWrap indentNonCollapsible">Essendant Associate</span></div></div>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
