ai didi

html - QuerySelector 中的单引号

转载 作者:行者123 更新时间:2023-12-04 21:50:51 24 4
gpt4 key购买 nike

HTML

<a onclick="loadYearPage('2018-2019')" style="cursor: pointer;">2018-2019</a>

如何使用 CSS 选择器定位元素 onclick

我试过下面的代码但不工作
ie.document.querySelector("[onclick='loadYearPage('2018-2019')']").Click

请建议

最佳答案

蒂姆的回答将是我的偏好,因为它很好地逃脱了。您还可以使用不同的运算符指定多个 css 属性 = 值选择器并结合 as 和语法

ie.document.querySelector("a[onclick^=loadYearPage][onclick*='2018-2019']")

如果这些子字符串中的任何一个只在页面上出现一次,您可以减少这种情况,例如
ie.document.querySelector("a[onclick*='2018-2019']")

使用您提供的页面,您确实可以使用 contains 修饰符 (*) 在 onclick 的值中按部分字符串搜索属性。下面是一个示例,您可以将年份作为变量传递。我已经选择了更短的选择器以获得更快的速度。需要稍微等待元素出现。我使用了定时循环。
Option Explicit  
'https://www.nseindia.com/products/content/derivatives/currency/cd_historical_businessGrowth.htm
Public Sub PerformClick()
Dim ie As InternetExplorer, t As Date, ele As Object
Const MAX_WAIT_SEC As Long = 10
Set ie = New InternetExplorer
Dim yearSelection As String
yearSelection = "2018-2019"
With ie
.Visible = True
.Navigate2 "https://www.nseindia.com/products/content/derivatives/currency/cd_historical_businessGrowth.htm"

While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer
Do
On Error Resume Next
Set ele = .document.querySelector("[onclick*='" & yearSelection & "']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
ele.Click
While .Busy Or .readyState < 4: DoEvents: Wend
End If
'other code
Stop '<==Delete me later
.Quit
End With
End Sub

关于html - QuerySelector 中的单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54341098/

24 4 0
文章推荐: excel - 打印相邻列中数字最大的文本
文章推荐: xcode - 在 Xcode 中,如何在 block 内添加断点?
文章推荐: .net - EF 代码首先插入多行
文章推荐: excel - 为什么在尝试设置范围时会收到 424 Object Required 错误?
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com