Selenium 发送 Enter 键返回错误“需要对象”

问题描述 投票:0回答:1

在 VBA 中使用 Selenium 自动填写 Edge 中的在线表单,但在尝试使用 SendKeys 进行 Enter 时收到错误消息。注意,没有提交按钮,需要输入。

Sub Login()
    'Open online trading platform
    Dim obj As New WebDriver
    obj.Start "edge", ""
    obj.Get "https://standard.tradezeroweb.us/"
    obj.Wait 1000
    obj.Window.Maximize
    obj.FindElementById("trading-order-input-symbol").Clear
    obj.FindElementById("trading-order-input-symbol").SendKeys ("AAPL")
    obj.FindElementById("trading-order-input-symbol").SendKeys (Keys.Enter) 'This is where the code fails
End Sub

Sendkeys (Keys.Enter) returns:
    Run-time error '424':
    Object required
Using obj.FindElementById("trading-order-input-symbol").SendKeys ("AAPL").submit returns:
    Run-time error '7':
    NoSuchElementError
Using obj.FindElementByXPath("//*[@id=trading-order-input-symbol]").SendKeys (obj.Key.Enter) returns:
    Run-time error '438':
    Object doesn't support this property or method

寻求帮助以及可能出现的问题。

谢谢大家。

excel vba selenium-webdriver microsoft-edge sendkeys
1个回答
0
投票

假设“trading-order-input-symbol”元素存在,那么

obj.FindElementById("trading-order-input-symbol").SendKeys (Keys.Enter)
的问题(导致错误 424)是
Keys
默认情况下不存在...必须声明它。

因此,在该行之前添加一行

Dim Keys As New Selenium.Keys

仅供参考,根据您遇到的错误,我猜您的模块顶部没有

Option Explicit
。这是“隐藏”问题......您应该始终使用
Option Explicit
。如果您添加此内容(不添加
Dim Keys As New Selenium.Keys
行)并编译您的项目,VBE 将告诉您“Keys”未定义 - 为您提供有关问题所在的线索。

© www.soinside.com 2019 - 2024. All rights reserved.