识别"css"的Excel IE Automation元素 [英] Excel IE Automation identifying a "css" element

查看:42
本文介绍了识别"css"的Excel IE Automation元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然在抓取网站上掠夺,从网页上打印等等,但是遇到了麻烦,我希望有人可以帮助我.下图显示了我想激活的选项卡,并且我有代码可以立即启动该选项卡,但无法激活该选项卡.尽管最后一行代码可以在我的台式机上运行,​​但我无法在其他PC上运行它.我可能可以将IE并置在另一台PC上,以使其正常工作,但我知道我只是在识别错误​​的元素,如果我正确地做到了,它应该一直工作,而不必弄乱IE设置.

Still plundering along scraping web sites, printing from web pages, etc. but ran into a snag I'm hoping someone can help me with. The pic below is shows the tab that I'd like to activate and I have the code to get me right there but can't activate the tab. While the last line of code will work on my desktop PC, I can't get it to work on my other PC's. I likely could juxtapose IE on the other PC's to make it work but I know that I'm just identifying the element wrong and if I did it correctly it should work all the time without having to mess with the IE settings.

带有"位置报告"的网站图片;标签在下面

A pic of the site with the "Location Report" tab is below

在其他浏览器上使用Selenium,我能够确定该标签的标识如下:

Using Selenium on a different browser I was able to determine that the tab was identified as below::

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

我要说明的代码在这里:

My code to get me to that point is here:

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"元素(或它们是什么)继续存在,因此我们将不胜感激.

So my question is what is the correct way to identify that "ext-gen42" element so that I can click on or press it to have that tab display so that I can scrape the information off of it?. I'm sure I'll run into more "css" elements (or whatever they are) as I continue so any help would be greatly appreciated.

推荐答案

如果click功能无法正常工作,您可以尝试更改标签的类别和标签内容的类别以激活位置报告标签.

If the click function can't work, you could try to change the class of the tabs and the class of the tab contents to active the Location Report tab.

您可以将位置报告标签类更改为活动状态,并隐藏传奇标签内容.请尝试替换无法正常工作的点击"的一部分:

You can change the Location Report tab class to active and hide the Legend tab content. Please try to replace the not working "click" part to this:

doc.getElementById("ext-comp-1065__ext-comp-1004").setAttribute "class", ""
doc.getElementById("ext-comp-1065__locationReport").setAttribute "class", "x-tab-strip-active"
doc.getElementById("ext-comp-1004").setAttribute "class", "x-panel x-panel-noborder x-hide-display"
doc.getElementById("locationReport").setAttribute "class", "x-panel x-panel-noborder"

您可以在IE中检查结果.

这篇关于识别"css"的Excel IE Automation元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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