如何在VBA中单击网页上的链接 [英] How in vba to click a link on a web page

查看:590
本文介绍了如何在VBA中单击网页上的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动在网站上登录。到目前为止,我设法让脚本键入了用户名和密码,还单击了登录按钮。这工作正常,我已经登录到该网站。
下一步将是单击一些链接以转到正确的页面,在该页面上我可以输入网站需要为我查找数据的搜索数据。

I am trying to automate the login on a website. So far I managed to let the script type in a user-name and password and also click on a login button. This works just fine and I am logged into the website. Next step would be to click on some links to get to the right page where I can enter the search data the site needs to find the data for me.

这是所涉及标签的HTML代码:

This is the HTML-code of the tag involved:

<a title="Klik hier voor de dienst Kadaster-on-line" class="serviceAvailable" href="https://kadaster-on-line.kadaster.nl/default.asp" target="_self" ng-if="!menuItem.items" ng-repeat-start="menuItem in menuItems">Kadaster-on-line</a>

我一直试图让VBA单击链接,但无法正确进行。这是我的最新尝试:

I have been trying to get VBA to click on a link but can't get in right. This is my latest attempt:

Set alle_keuzes = IE.document.getElementsByTagName("a")

For Each keuze_voor_kadaster In alle_keuzes        
    If keuze_voor_kadaster.getAttribute("title") = "Klik hier voor de dienst Kadaster-on-line" Then
        keuze_voor_kadaster.Click
        Exit For
    End If
Next keuze_voor_kadaster

这样做的正确方法是什么?

What would be a proper way to do this?

推荐答案

链接应由

ie.document.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']").click

您也可以尝试:

ie.document.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']").FireEvent "onclick"

出现未设置/发现错误的对象,则:

If you are getting an object not set/found error then:

1)检查html以查看ifparent frame / iframe标签是哪个在其中找到ou元素。

1) Inspect the html to see ifparent frame/iframe tag which you element is found inside of.

在这种情况下,您可能需要类似以下语法:

In that case you may need syntax similar to:

ie.document.document.getElementsByTagName("frame")(frameIndexGoesHere).contentDocument.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']")

如果框架/ iframe具有ID,请使用 getElementById

Use getElementById if the frame/iframe has an id.

2)检查您等待了足够长的时间,以便元素在出现之前单击。如果是这种情况,请确保您具有以下页面加载和循环,以等待元素出现:

2) Check you are waiting long enough for the element to be present before clicking. If that is the case ensure you have the following for page load and a loop to wait for element to be present:

导航到URL之后

While ie.Busy Or ie.readyState < 4: DoEvents: Wend

Const MAX_WAIT_SEC As Long = 5
Dim ele As Object, t As Date
t = Timer
Do
    DoEvents
    On Error Resume Next
    Set ele = ie.document.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']")
    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

这篇关于如何在VBA中单击网页上的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆