我正在尝试在Excel中创建一个VBA宏:
一切正常,直到第5步。当我尝试搜索HTML文档时,由于某种原因它不会搜索第二页的HTML文档,而是查看步骤2中初始网页的HTML和在步骤3中打印出相同的结果。
请问你们看看我的代码,看看我做错了什么?我在下面列出了我的代码并尝试发表评论以使其可读。
Sub C_R()
Dim ie As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
'Opens Internet Explorer and navigates to website.
ie.Visible = True
ie.navigate "http://openaccess.sb-court.org/OpenAccess/"
Do While ie.ReadyState <> READYSTATE_COMPLETE
Loop
'Searches HTML(initial page) to find all elements with "input" tag name.
'Prints attributes of each element (name, type, and value".
Set HTMLDoc = ie.Document
Set HTMLButtons = HTMLDoc.getElementsByTagName("input")
Debug.Print "Initial Page"
For Each HTMLButton In HTMLButtons
Debug.Print HTMLButton.getAttribute("name"), HTMLButton.getAttribute("type"), HTMLButton.getAttribute("value")
Next HTMLButton
'Navigates to second page
HTMLButtons(1).Click
Do While ie.ReadyState <> READYSTATE_COMPLETE
Loop
'Searches HTML(second page) to find all elements with "input" tag name.
'Prints attributes of each element (name, type, and value".
Set HTMLDoc = ie.Document
Set HTMLButtons = HTMLDoc.getElementsByTagName("input")
Debug.Print "Second Page"
For Each HTMLButton In HTMLButtons
Debug.Print HTMLButton.getAttribute("name"), HTMLButton.getAttribute("type"), HTMLButton.getAttribute("value")
Next HTMLButton
End Sub
我们将非常感谢您提供的任何帮助。非常感谢你。
答案 0 :(得分:0)
即使网页没有完全加载,看起来你的第二个循环仍在继续。
public static int[] primes(BitSet composite) {
int size = composite.size() - 2 - composite.cardinality();
int[] primes = new int[size];
int index = 0;
for (int i = 2; i < composite.size(); i++) {
if (!composite.get(i)) primes[index++] = i;
}
return primes;
}
要看到这一点,请在第二个循环中设置一个断点,然后等待加载第二个网页。然后继续代码,它应该可以正常工作。
您需要在循环中添加等待时间,这可能并不总是有效,或者找到另一种方法来判断ie.ReadyState是否完整。