如何获取网页<form>标签内的元素?

时间:2018-03-06 18:33:07

标签: excel vba excel-vba

我想访问一个位于网站“form”标签内的按钮,如果我打印表单的内部文本我可以获得整个页面但我不知道如何访问内部的元素形式。

代码尝试了:

Dim workFrame As HTMLFormElement, test As HTMLFormElement
Dim HTMLDoc As HTMLDocument
Set workFrame = ie2.Document.forms("ViewReferral")
Debug.Print workFrame.innerText ' it will print the whole page
Debug.Print workFrame.elements("notsuccess").innerText ' this triggered an error

网页

<form name="ViewReferral" action="viewreferral.php" method="POST">
<input name="Id" id="Id" type="hidden" value="11111">
<input name="Message" id="Message" type="hidden" value="">
 <div id="data-form" style="padding: 10px;">

    <div style="padding: 10px; float: left;"><h1>View Referral (Id = 11111)</h1></div>

        <div class="order">
            <a class="ui-button ui-corner-all ui-widget"role="button" style="float: right; display: inline;" href="managedocuments.php">Back</a>
            <a class="ui-button ui-corner-all ui-widget" role="button" style="float: right; display: inline;" href="vieworder.pdf.php?OrderId=111111" target="_blank">Print</a>
                <a class="VoidOrder ui-button ui-corner-all ui-widget" role="button" style="float: right; display: inline;" href="#1111"> Void</a>
                        <a class="ui-button ui-corner-all ui-widget" role="button" style="float: right; display: inline;" href="markasprocessed.php?OrderId=11111">Mark as Processed</a>

这里我想激活href =“vieworder.pdf.php?
我无法访问网络表格中的表格,请帮我解决这个问题 谢谢。

2 个答案:

答案 0 :(得分:1)

正式化我的评论,您应该尝试使用getElementsByClassName,它返回文档或表单中包含给定类的每个元素的数组。在您的情况下,由于您要使用的<a>标记是第"ui-button ui-corner-all ui-widget"类的第二个标记,并且是workFrame形式,因此适合您的代码行是:

workFrame.getElementsByClassName("ui-button ui-corner-all ui-widget") (1)

其中(1)告诉程序返回数组中的第二项(数组从0开始索引)。

答案 1 :(得分:1)

使用

的评论中给出了一个不错的CSS选择器
.Document.querySelector("a[href*='markasprocessed.php']")

*表示包含。因此,a标记的href包含文字字符串'markasprocessed.php

或者

.Document.querySelector("a[href^='markasprocessed.php']")

^表示开头。因此a标记的href标记以文字字符串'markasprocessed.php开头

css query 1

您还可以使用不太健壮的基于位置的选择器:

.Document.querySelector("a[href]:nth-child(4)")

CSS selector

包含a

的第4个href.代码
相关问题