【问题标题】:Automate IE with VBA - Click anchor button with span使用 VBA 自动化 IE - 单击具有跨度的锚按钮
【发布时间】:2016-01-10 13:25:35
【问题描述】:

我目前正在为在 saleforce 中上传文件进行自动化操作。

HTML 文本是:

 <div>
<a href="javascript:void(0);" onblur="chatter.getFeed().contentCommentActionBlur(this);" onfocus="chatter.getFeed().contentCommentActionFocus(this);" onclick="chatter.getFeed().createContentComment(this,'0D52700002cc67d', false, true);" onmousedown="chatter.getFeed().contentCommentActionMouseDown(this);" class="cxcontentcommentaction">
<img class="cxcontentcommentactionimg" src="/s.gif">
<span class="cxcontentcommentactiontext">Attach File</span>
</a>
</div>

点击锚点的用户需要向下按“附加文件”按钮。

我尝试了两种不同的做法

1.编码通过anchor class="cxcontentcommentaction"获取元素:

Set htmldoc = mydoc.getElementsByClassName("cxcontentcommentaction")
    htmldoc.Click

2.Coding通过span class="cxcontentcommentactiontext"获取元素:

Set htmldoc = mydoc.getElementsByClassName("cxcontentcommentactiontext")
    htmldoc.Click

对于这两个我都收到错误“对象不支持此属性

在你的评论之后,我试过这样,

Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentactiontext")

    For Each link In oElementCollection
    If link.innerHTML = "Attach File" Then
        link.Click
        Exit For
    End If
Next link

我已经尝试过以下 HTML

<a href="javascript:window.invokeOnClickJS_00bG0000000ikPK%28this%29" class="menuButtonMenuLink">
Send an Email</a>

下面的代码对上面的html正常工作

Set oElementCollection = mydoc.getElementsByClassName("menuButtonMenuLink")


    For Each link In oElementCollection
    If link.innerHTML = "Send an Email" Then
        link.Click
        Exit For
    End If
Next link

但是这个问题中提到的“附加文件”按钮只会出现问题。

【问题讨论】:

  • 看起来这应该是表单元素的一部分 - 在这种情况下,只需使用 Forms(0).Submit0 更改为正确的索引。

标签: vba excel internet-explorer automation


【解决方案1】:

getElementsByClassName 方法返回具有此类名称的元素集合。这是因为 HTML DOM 可以包含多个具有相同类名的元素。这就是复数形式 getElements 的原因。所以你需要知道你需要的元素集合中的哪个元素。如果是第一个,那么:

...
Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentaction")
oElementCollection(0).Click
...

如果第一个不正确,那就试试

For i = 0 To oElementCollection.Length - 1
 MsgBox oElementCollection(i).innerHTML
Next

找到合适的。

编辑:

在您在问题中添加之后:

Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentactiontext") 

您可能会得到span 对象而不是链接的集合。但是那些span 元素应该在链接中。所以链接是他们的父母。

所以

Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentactiontext")

    For Each span In oElementCollection
    If span.innerHTML = "Attach File" Then
        span.parentNode.Click
        Exit For
    End If
Next span

【讨论】:

  • 请检查所有找到的元素的innerHTML 以获得正确的元素。
  • @Kathirvtv 除了上面说的 Axel 之外,还请考虑您循环通过的每个对象 (oElementCollection(i)) 都可以使用 .getAttribute('name of the attribute') 函数进行分析,该函数返回您的某个属性的值物体。这可能有助于扫描例如 on-action 属性调用它可能正在上传您的文件并单击正确的 javascript。
  • 附加屏幕无法正常打开也无法导航,我认为这是因为附加文件按钮可用页面末尾,因此用户需要手动向下并按下按钮。
  • @Kathirvtv 如果该按钮“附加文件”在页面中,它也在您的 HTML 文档的 DOM 中。您可以通过 VBA 单击它,但如果它打开一个框架,我不确定您是否能够通过同一个浏览器实例导航它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2020-03-07
  • 2020-09-15
  • 2015-07-06
  • 2019-11-30
相关资源
最近更新 更多