【问题标题】:I'm not able to click a link using querySelector我无法使用 querySelector 单击链接
【发布时间】:2021-02-03 11:15:56
【问题描述】:

我想通过选择特定日期从网站下载文件。检查显示 href 内容的元素,但无法点击它。

网页代码

<td>
<a href="/content/historical/EQUITIES/2021/FEB/cm02FEB2021bhav.csv.zip" target="new">cm02FEB2021bhav.csv.zip</a><br>
</td>

我的 VBA 代码

Dim files As String

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm"
IE.Visible = True
While IE.readystate <> 4: DoEvents: Wend


Do

Set el = Nothing
On Error Resume Next
Set el = IE.document.getelementbyid("h_filetype")
On Error GoTo 0
DoEvents
Loop While el Is Nothing
IE.document.getelementbyid("h_filetype").Value = "eqbhav"

Do

Set el = Nothing
On Error Resume Next
Set el = IE.document.getelementbyid("date")
On Error GoTo 0
DoEvents
Loop While el Is Nothing
IE.document.getelementbyid("date").Value = "02-02-2021

IE.document.parentWindow.execScript "validateInput();", "javascript"

IE.document.querySelector("a[href^='/content/historical/EQUITIES/']").Click

我已经尝试了下面的代码,但它不起作用。

IE.document.querySelector("a[href^='/content/historical/EQUITIES/']").Click

【问题讨论】:

  • 另外,除了说“不工作”,你能补充更多细节吗?是否有任何错误信息?如果有,是什么,在哪里?

标签: html excel vba


【解决方案1】:

您的 QuerySelector .querySelector("a[href^='/content/historical/EQUITIES/']") 无法工作,因为 /content/historical/EQUITIES/ 只是 url 的一部分。另一个原因是,在执行完 JS 之后,你必须让文档有机会生成想要的 url。

我已经完成了您的代码,直到 Click。然后,您必须再次等待一段时间,以便在 IE 底部显示带有保存按钮的栏。然后你必须使用SendKeys() 来触发保存。然后该文件将在 IE 的默认下载文件夹中结束。如果没有设置额外的文件夹,这是系统文件夹Downloads

Sub TestClick()

Dim files As String
Dim IE As Object
Dim el As Object

  Set IE = CreateObject("InternetExplorer.Application")
  IE.navigate "https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm"
  IE.Visible = True
  While IE.readystate <> 4: DoEvents: Wend
  
  Do
    Set el = Nothing
    On Error Resume Next
    Set el = IE.document.getelementbyid("h_filetype")
    On Error GoTo 0
    DoEvents
  Loop While el Is Nothing
  IE.document.getelementbyid("h_filetype").Value = "eqbhav"
  Do
    Set el = Nothing
    On Error Resume Next
    Set el = IE.document.getelementbyid("date")
    On Error GoTo 0
    DoEvents
  Loop While el Is Nothing
  IE.document.getelementbyid("date").Value = "02-02-2021"
  IE.document.parentWindow.execScript "validateInput();", "javascript"
  
  'You must spend time to generate the link you want
  Do
    Set el = Nothing
    On Error Resume Next
    Set el = IE.document.getelementbyid("spanDisplayBox").getElementsByTagName("a")(0)
    On Error GoTo 0
    DoEvents
  Loop While el Is Nothing
  'Than you can click it
  el.Click
  
  'Here more of your code
  '...
End Sub

【讨论】:

  • 我今天早些时候没有时间检查 html,但再看一遍,我发现 OP 应该使用 contains * 运算符,而不是以 ^ 运算符开头,即ie.Navigate IE.document.querySelector("a[href*='/content/historical/EQUITIES/']").href
  • @QHarr 谢谢你的解释。我不知道运营商。这就是我用 get 方法解决它的原因。
  • 这很酷。我仅在您想扩展您的答案时发表评论,您已经注意到当前尝试由于部分字符串而失败。只是他们希望 href 开始是 \..... 因为他们使用了 ^ 运算符。
猜你喜欢
  • 2017-10-20
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多