VBA : Object Required


Option Explicit
Private ChBrowser As Selenium.ChromeDriver

Sub LogInToScreener()

Dim FindBy As New Selenium.By
Set ChBrowser = New Selenium.ChromeDriver
Dim ButtonElement As Selenium.WebElement
Dim InputElementCollection As Selenium.WebElements
Dim InputElement As Selenium.WebElement
ChBrowser.Start baseUrl:="https://www.screener.in/login"
ChBrowser.get "/?"
' Username - Element exists?
If Not ChBrowser.IsElementPresent(FindBy.name("username"), 3000) Then
ChBrowser.Quit
MsgBox "Could not find Username input field", vbExclamation
Exit Sub
End If
ChBrowser.FindElementByName("username").SendKeys "iaea290857@gmail.com"

' Password - Element exists?
If Not ChBrowser.IsElementPresent(FindBy.name("password"), 3000) Then
ChBrowser.Quit
MsgBox "Could not find Password input field", vbExclamation
Exit Sub
End If
ChBrowser.FindElementByName("password").SendKeys "screener_4102"


' =======================
' Sending 'TAB' to Chrome
' =======================

Application.SendKeys "{TAB}"


' ==================================================
' Check whether the ACTIVE ELEMENT is LOGIN (button)
' ==================================================

Set ButtonElement = ChBrowser.activeElement

'MsgBox "The active element : " & Element.Text


' =======================
' Button - Button exists?
' =======================

If Not ChBrowser.IsElementPresent(FindBy.Class("button-primary"), 3000) Then
ChBrowser.Quit
MsgBox "Could not find the Login button", vbExclamation
Exit Sub

Else
ButtonElement.Click

End If
Debug.Print ChBrowser.URL

If ChBrowser.URL = "https://www.screener.in/dash/" Then
If ChBrowser.FindElementsByTag("input").Count > 0 Then

' Prints body

Debug.Print ChBrowser.activeElement.tagName


' ============
' Collection :
' ============

' Set InputElementCollection = ChBrowser.FindElementsByTag("input")

' ========================================
' Prints 3 - But 'Index' starts from 0,1,2
' ========================================

' Debug.Print ChBrowser.FindElementsByTag("input").Count

Set InputElement = ChBrowser.FindElementsByTag("input")(1)

Error: Object Required

1。如何引用:Input Element' (https://www.screener.in/dash/) ?
2。如何输入值="除法";进入搜索框?

ChBrowser.FindElementsByTag("input")(1).Value = "Divis"

End If

End If
End Sub

注意:我可以使用用户名和密码登录。如何"进入一家公司"(问题2:见上文)在搜索框(https://www.screener.in/dash/)

答案1

我引用了错误的索引输入元素

Set InputElement = ChBrowser.FindElementsByTag("input")(2)

Debug.Print "TagName of the <element> : " & InputElement.tagName

打印-即时窗口中的"输入">

回答2

InputElement.sendKeys("Divis")

点击"这里"查看期望结果

相关内容