首页 > 解决方案 > 如何使用 Selenium 单击 HREF

问题描述

尝试使用 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

标签: vbaselenium-webdriverweb-scraping

解决方案


我将在 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

推荐阅读