Excel VBA选择打开网页中显示的第一个URL

时间:2018-03-25 11:03:42

标签: vba excel-vba excel

我已经在这里得到了一些非常支持的人的支持,创建了下面的代码,现在我希望vba点击页面中显示的第一个网址"下面提到的网络代码",然后从表格中提取一些数据将被点击此链接后。:(或者,如果我可以点击第一个图像那里

"它是一个内联网网址"

Sub Test()

Dim rng As Range
Set rng = Sheets("sheet1").Range("A1", Sheets("sheet1").Cells.Range("A1").End(xlDown))
Dim ie As Object

For Each cell In rng

    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("login URL")
    Do
        If ie.ReadyState = 4 Then
            ie.Visible = False
            Exit Do
        Else
            DoEvents
        End If
    Loop

    ie.Document.forms(0).all("txtUsername").Value = ""
    ie.Document.forms(0).all("txtPassword").Value = ""
    ie.Document.forms(0).submit
    ie.Visible = True

    Application.Wait (Now + TimeValue("00:00:02"))

    With ie
        .Visible = True
        .Navigate "search page URL"
        While .Busy = True Or .ReadyState < 4: DoEvents: Wend

        For Each Post In ie.Document.getElementsByName("cboFieldName")(0).getElementsByTagName("option")
            If InStr(Post.innerText, "Global Service Reference") > 0 Then Post.Selected = True:

            ie.Document.forms(0).all("txtFieldValue").Value = cell.Value

            ie.Document.forms(0).submit


        Next Post

        DoEvents

    End With


    Application.Wait (Now + TimeValue("00:00:02"))


Next cell


End Sub

以下是网络代码。

<p>
    <font size="3" face="cambria'" color="#e60000">
        <b></b>
    </font>
    <font size="2" face="Tahoma">
        <a href="service/service.asp?id=107210501#items">
            <img src="icons/plus.gif" border="0" align="top" width="19" height="19">
        </a>
        <font size="2" face="Tahoma">
            <a href="service/service.asp?id=107210501">
                <img src="icons/service.gif" border="0" align="top">ASTLX-IPQM-00150
            </a>
            <br>
        </font>
    </font>
</p>

2 个答案:

答案 0 :(得分:2)

如果点击任何图片链接是您唯一的要求,您可以尝试以下操作。

Dim IE as New InternetExplorer, post as Object

Set post = IE.document.querySelector("img[src*='plus.gif']") ''for first image link
'Set post = IE.document.querySelector("img[src*='service.gif']") ''for second image link
post.Click

我已经注释掉了第二个图片链接,因为我不确定你想要去哪个链接。

或者,您也可以尝试这样:

For Each post In IE.document.getElementsByTagName("a")
    If InStr(post.getElementsByTagName("img")(0).src, "service.gif") > 0 Then post.Click: Exit For
Next post

答案 1 :(得分:0)

Without seeing the website it's a bit difficult to see precisely what you need. Given your HTML code, it seems you're trying to navigate the relative link service/service.asp?id=107210501 (which is probably dynamic) - and the way you have to recognize it is by an image with a "service" (at least by the name of the source icons/service.gif).

If that's the case, you might try to:

  • Get all the img elements in the page (Set allImg = ie.document.getElementsByTagName("img"))
  • Loop through them until when you find the one having source icons/service.gif and, once you find it, navigating the link wrapping it. This can be done with:

...

link = "www.your-intranet-website.com"
For j = 0 To allImg.Length
    If allImg(j).getAttribute("src") = "icons/service.gif" Then
        link = link & "/" & allImg(j).parentElement.getAttribute("href")
        Exit For     
    End If
Next j
ie.Navigate link

I guess a better solution might be provided if you could share at least a screenshot of your website and the full HTML source.