【问题标题】:Click on HREF that begins with a string单击以字符串开头的 HREF
【发布时间】:2020-01-30 14:43:33
【问题描述】:

我在尝试将 href 点击进入网页时遇到问题。 href 包含在“a”标签中,它以“http://trial.xxxyyy”开头。如何设置允许我单击以前面提到的字符串开头的 href 的字符串?我认为 queryselector 是关键,但我不熟悉它。

P.s 页面很空,所以没有太多的 html。

【问题讨论】:

  • 请记住包含您的代码

标签: vba internet-explorer web-scraping href


【解决方案1】:

尝试使用[attribute^=value]CSS selector,它将选择所有href属性值以特殊值开头的元素。

示例代码如下:

Public Sub ClickTest()

    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "<the website url>"

        While .Busy Or .readyState <> 4: DoEvents: Wend

        ie.Document.querySelector("a[href^='https://www.bing']").Click

    End With
End Sub

这样的网站资源:

<a class="link" href="https://www.google.com/"> Google</a><br />
<a class="link" href="https://www.microsoft.com/en-us"> Microsoft</a><br />
<a class="link" href="https://www.bing.com/"> Bing</a><br />
<a class="link" href="https://www.bing.com/search?q=vba&qs=n&form=QBLH&sp=-1&pq=vba&sc=6-3&sk=&cvid=400A0A54265148289028B12BE5D9E3A4">Bing search</a>

[注意]如果有多个元素匹配过滤器,使用上面的代码,它会得到第一项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2011-04-03
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多