使用VBA单击链接

时间:2018-03-26 11:36:21

标签: excel vba excel-vba

我正在尝试单击链接以使用VBA查看页面上的信息,然后继续使用VBA编辑该信息,但试图找出如何为ID编写代码随着每个ID信息的变化搜索,

有关于此的任何想法吗?

我环顾四周,似乎无法理解如何让VBA选择这一行作为(ID =)并且它没有加入我正在搜索的相同ID。

还有serval参考

这是代码行。

names(nt)[4] == names(pc.tbl)[2]

[1] TRUE 

这是我当前搜索它的代码。没有点击视图部分。

<a href="/?do_Action=ViewEntity&amp;Entity_ID=14287">View</a>

被修改

我在代码中添加了我认为它应该遵循该链接并进行一些修改以使其不会出现编译错误:D,All似乎工作但是当它到达行时单击链接它不会失败,但甚至不会点击它。然后它将转到备注表中列表中的下一个,这是预期的。

通过使用调试器显示没有错误或失败的内容,如果代码错误或链接,我希望它能做什么,

请帮忙吗?

这是现在的代码

Sub Test()
    Dim ie As Object
    Dim form As Variant
    Dim button As Variant
    Dim LR As Integer
    Dim var As String

    LR = Cells(Rows.Count, 1).End(xlUp).Row

    For x = 2 To LR
        var = Cells(x, 1).Value
        Set ie = CreateObject("internetexplorer.application")
        ie.Visible = True

        With ie
            .Visible = True
            .navigate "*******"

            While Not .readyState = READYSTATE_COMPLETE
            Wend
        End With

        'Wait some to time for loading the page
        While ie.Busy
            DoEvents
        Wend

        Application.Wait (Now + TimeValue("0:00:02"))
        ie.document.getElementById("quicksearch").Value = var

        'code to click the button
        Set form = ie.document.getElementsByTagName("form")
        Application.Wait (Now + TimeValue("0:00:02"))
        Set button = form(0).onsubmit
        form(0).submit

        'wait for page to load
        While ie.Busy
            DoEvents
        Wend
    Next x
End Sub 

这是按钮周围代码的副本,

Sub Test1()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
Dim a
Dim linkhref
linkhref = "/?do_Action=ViewEntity&amp;Entity_ID"

With ie
    .Visible = True
    .navigate "*******"
    While Not .readyState = READYSTATE_COMPLETE
    Wend
End With
 'Wait some to time for loading the page
While ie.Busy
    DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("quicksearchbox").Value = var
 'code to click the button
  Set form = ie.document.getElementsByTagName("form")
  Application.Wait (Now + TimeValue("0:00:02"))
  Set button = form(0).onsubmit
  form(0).submit
  'wait for page to load
  While ie.Busy
    DoEvents
  Wend
   Application.Wait (Now + TimeValue("0:00:02"))
   For Each a In ie.document.getElementsByTagName("a")
   If (a.getAttribute("href")) = ("/?do_Action=ViewEntity&amp;Entity_ID=") Then
   a.Click
   Exit For
   Application.Wait (Now + TimeValue("0:00:02"))
   While ie.Busy
   DoEvents
   Wend
   End If
   Next
   Next x 


End Sub

谢谢。

2 个答案:

答案 0 :(得分:0)

如果元素href类似"/?do_Action=ViewEntity&amp;Entity_ID=14287",则此if语句永远不会评估为True

(a.getAttribute("href")) = ("/?do_Action=ViewEntity&amp;Entity_ID=")

因此永远不会点击该元素。

如果您知道要点击的Entity_ID,则可以执行以下操作:

Dim Entity_ID as Integer

Entity_ID = 14287
If (a.getAttribute("href")) = "/?do_Action=ViewEntity&amp;Entity_ID=" & Cstr(myID) Then a.click 

否则只需检查元素href是否包含该url:

If InStr(1, a.getAttribute("href"), linkhref) > 0 Then a.click

修改

好的,使用您发布的HTML,我可以通过此操作访问您请求的指定a标记

For Each ele In ie.document.getElementById("searchresults").getElementsByTagName("a")
   If InStr(1, ele.href, "do_Action=ViewEntity") > 0 Then
       MsgBox "The button is found!"
   End If
Next

答案 1 :(得分:0)

使用For检查每个<a>标记元素,并确保单击右边的元素。这是真的你的ID改变,但字符串的其余部分是不变的,所以这是1因素。此外,看起来你总是会点击它所说的View,这是另一个常数。

使用这两个选项,我们可以开发一个简单的For..Next,它将检查每个<a>元素,并检查是否满足这两个选项要求:

For Each a In ie.document.getElementsByTagName("a")
   If Left(a.href, 37) = "/?do_Action=ViewEntity&amp;Entity_ID=" And a.innerText = "View" Then
    a.Click
    Exit For
Next a

尝试一下,让我们看看这是否适合你。