仍在掠夺网站,从网页打印等,但遇到了一个障碍,我希望有人能帮助我。下面的图片显示了我想激活的选项卡,我有代码可以让我在那里但无法激活选项卡。虽然最后一行代码可以在我的台式电脑上运行,但我无法让它在我的其他电脑上运行。我可能可以将 IE 并置在另一台 PC 上以使其工作,但我知道我只是识别错误的元素,如果我做得正确,它应该一直工作,而不必弄乱 IE 设置。
在不同的浏览器上使用 Selenium,我能够确定该选项卡的标识如下:
css=#ext-gen42 .x-tab-strip-text
xpath=//a[@id='ext-gen42']/em/span/span
xpath=//div[3]/div/div/div/div/div/ul/li[2]/a[2]/em/span/span
我的代码让我达到这一点:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub
Sub Oasis_Test()
LocnAddr = "1 park Ave"
LocnBoro = "Manhattan"
Dim IE As Object
Dim htmlDoc As Object
Dim Field1 As Object
Dim Field2 As Object
Dim Field3 As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.oasisnyc.net/map.aspx"
Do Until IE.readyState = 4: DoEvents: Loop
Application.Wait (Now + TimeSerial(0, 0, 3))
'ShowWindow IE.hWnd, SW_SHOWMAXIMIZED ' off for test purposes
Set htmlDoc = IE.document
On Error Resume Next
Set Field1 = htmlDoc.getElementById("ext-comp-1076")
On Error GoTo 0
Call TriggerEvent(htmlDoc, Field1, "compositionstart")
Field1.value = LocnAddr
Call TriggerEvent(htmlDoc, Field1, "compositionend")
Application.Wait (Now + TimeSerial(0, 0, 2))
' there's probably a better way to do this drop-down but this works
Set Field2 = htmlDoc.getElementById("ext-comp-1078")
Call TriggerEvent(htmlDoc, Field2, "compositionstart")
Field2.value = LocnBoro
Call TriggerEvent(htmlDoc, Field2, "compositionend")
Application.SendKeys "{TAB}"
Application.Wait (Now + TimeSerial(0, 0, 2))
htmlDoc.getElementById("ext-gen118").Click
Application.Wait (Now + TimeSerial(0, 0, 2))
' I don't get an error on the next line and it actually works on my desktop
' but can't get it to work anywhere else.
htmlDoc.getElementById("ext-gen42").Click
Application.Wait (Now + TimeSerial(0, 0, 2))
htmlDoc.getElementById("ext-gen44").Click
Application.Wait (Now + TimeSerial(0, 0, 2))
htmlDoc.getElementById("ext-gen46").Click
Application.Wait (Now + TimeSerial(0, 0, 2))
htmlDoc.getElementById("ext-gen42").Click '##### NG ######
End Sub
所以我的问题是识别“ext-gen42”元素的正确方法是什么,以便我可以单击或按下它以显示该选项卡,以便我可以从中刮掉信息?我相信我会在继续的过程中遇到更多的“css”元素(或其他任何元素),因此我们将不胜感激。