【问题标题】:Clicking a button in IE with VBS when the button has no ID or name and has the same class as another button当按钮没有ID或名称并且与另一个按钮具有相同的类时,在带有VBS的IE中单击按钮
【发布时间】:2022-01-18 12:16:15
【问题描述】:

我正在尝试使用 VBS 在 IE 中自动执行一项任务,我需要单击提交按钮。有两个提交按钮,一个“搜索”和一个“Phoenetic Search”。两个按钮都没有指定值或 id,并且它们都具有相同的类。按钮的 HTML 是:

<td align="left"><button type="button" class="searchAdv" onclick="javascript:postAdvancedSearch()">Search</button><br></td>
<td align="left"><button type="button" class="searchAdv" onclick="javascript:postAdvancedPhoneticSearch()">Phonetic Search</button></td>

到目前为止我有这个代码:

Set IE = createobject("internetexplorer.application") 
strURL = "http://somewebsite.com"
IE.navigate strURL 
IE.Visible = true   
Do While (IE.Busy Or IE.READYSTATE <> 4)    
    WScript.Sleep 100    
Loop    
Set searchButton = IE.Document.GetElementsByClassName("searchAdv")

这会抓住两个按钮,因为它们是页面上唯一具有该类的按钮,但现在我如何选择正确的按钮来单击。 searchButton(0 or 1).value 为空。什么属性包含按钮的“搜索”和“Phoenetic Search”标签?我认为会有类似searchButton(0).label.text 之类的东西,但我还没有找到有效的方法。如何抓住右键?

【问题讨论】:

    标签: html internet-explorer vbscript


    【解决方案1】:

    想通了。这是innerhtml 属性。

    for each button in searchButton
        if instr(button.innerhtml,"Search") then
            button.click
            exit for
        end if
    next
    

    【讨论】:

    • 该检查匹配“搜索”“语音搜索”,因为两者都包含(子)字符串“搜索”,所以点击的按钮取决于哪个@987654323 @ 首先返回。最好使用innerText 属性并比较完整的字符串:If LCase(Trim(button.innerText)) = "search" ThenTrim() 从字符串中删除前导和尾随空格,并通过使用 LCase() 模拟不区分大小写的比较。
    【解决方案2】:

    或者通过循环浏览页面上的所有链接:

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = 1
    'IE.Visible = 0
    IE.navigate "http://<www.YourUrlToPage.net/..>"
    While IE.ReadyState < 4 Or IE.Busy : WScript.Sleep 100 : Wend
    '(ReadyState:4=Complete,3=Interactive,2=Loaded,1=Loading,0=Uninitialized)
    
    'WScript.Echo IE.Document.title
    For Each link in IE.Document.links
      'a=MsgBox("link:" & vbCrLf & " " & link.href & vbCrLf &_
      '         "html:" & vbCrLf &_" " & link.innerhtml & vbCrLf &_
      '         "text:" & vbCrLf &_" " & link.innertext)
      If instr(link.innerhtml,"<check text>") Then
        link.Click
        While IE.ReadyState < 4 Or IE.Busy : WScript.Sleep 100 : Wend
        Exit For
      End If
    Next
    

    (激活 MsgBox 去掉相关的单引号)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2016-04-26
      • 2015-11-02
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多