【问题标题】:How to click HREF using Selenium如何使用 Selenium 单击 HREF
【发布时间】:2019-11-29 23:25:59
【问题描述】:

尝试使用 Selenium 类型库在 Chrome 上单击下载按钮。下面的代码是我从板上拾取的,但我收到了语法错误

Sub Test()
Dim bot
Set bot = CreateObject("Selenium.WebDriver")

bot.Start "Chrome", "https://www.afterpaytouch.com"
bot.get "/results-reports"

bot.findElement(By.linkText("https://www.afterpaytouch.com/images/28082019-FY2019-Results-Presentation.pdf")).click()

End Sub

【问题讨论】:

  • 您可能希望将bot 调暗为`Object` 而不是假定的Variant,但这可能无法解决问题。请解释您在哪一行得到错误。你忘了在你的问题中包含这个。
  • 错误出现在bot.FindElement行 - 我可以正常打开网页但无法点击下载按钮
  • 与其尝试在一行中全部单击,不如先将元素设置为变量,然后检查变量是否首先设置为某个值。如果没有,则它无法找到该元素。我最好的猜测是您不想包含元素的整个路径。 - techbeamers.com/findelement-and-findelements-commands-examples
  • 请同时发布语法错误。
  • 尝试将元素设置为变量,但我收到“未定义子或函数错误”```Sub Test() Dim bot Set bot = CreateObject("Selenium.WebDriver") Dim元素集 Element = FindElement(By.linkText("afterpaytouch.com/images/…))

标签: vba selenium-webdriver web-scraping


【解决方案1】:

我会在 VBE > Tools > References 中添加对 Selenium 类型库的引用,然后使用早期绑定引用、完整 url 并应用 VBA selenium 基本语法通过 css 查找链接并单击

Option Explicit

Public Sub Test()
 Dim bot As WebDriver
    Set bot = New ChromeDriver

    With bot
        .Start "Chrome"
        .get "https://www.afterpaytouch.com/results-reports"
        .FindElementByCss("[href='https://www.afterpaytouch.com/images/28082019-FY2019-Results-Presentation.pdf']").Click
        Stop '<delete me later
    End With
End Sub

【讨论】:

    猜你喜欢
    • 2015-08-18
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2016-11-06
    • 1970-01-01
    • 2023-01-08
    • 2020-07-14
    相关资源
    最近更新 更多