如何使用Selenium VBA捕获innerHTML

问题描述 投票:2回答:1

[我试图在Chrome浏览器中执行任务时弹出一个弹出窗口,然后基于innerHTML单击确定并保存或退出。我遇到的问题是在iframe中可能出现的弹出窗口之前,需要切换回主体。我正在使用硒:

main body (possible pop_up *i think*)
   iframe (I am here)

下面是我的一些代码,删除了不必要的行:

Dim sel As Selenium.ChromeDriver
Dim pop as Object    

Set sel = New Selenium.ChromeDriver

sel.Get URL
' Switch to iframe
Set iframe = sel.FindElementByTag("iframe", 10000)
sel.SwitchToFrame iframe, 5000

code...

' Where the popups sometimes show up
On Error Resume Next
sel.SwitchToDefaultContent
Set pop = sel.FindElementsByClass("popupText", 2000) *Error Could not locate class=popupText
On error goto 0

即使关闭弹出窗口,如果下次关闭错误恢复,也会出现此错误。以下是HTML:

<span class="popupText">
  <br />
  *Text* 
  <br />
  <br />
  *Text*
</span>

我已经尝试了其他几件事:不使用switchtodefaultcontent,而使用switchtoparenframe似乎没有任何效果。

硒中还有一个.SwitchToAlert功能也不起作用...

strAlert = sel.SwitchToAlert.Text 'Get nothing
sel.SwitchToAlert.Accept          'Does not do anything
excel vba selenium
1个回答
0
投票

找到了解决方法,似乎硒vba插件有问题。解决方法:

sel.SwitchToDefaultContent
inner_text = Right(sel.PageSource, 1000)

相反,我在切换到默认内容后突然抓起了page_source(弹出文本恰好在html的最后),然后我继续检查inner_text变量中是否有弹出窗口(4个不同的pop_up选项)。然后从那里继续。

我认为这是VBA Selenium插件存在问题的原因是我使用Python复制了上面的代码,并且效果很好...所以不确定那里发生了什么。 VBA Selenium的日期约为4年。

© www.soinside.com 2019 - 2024. All rights reserved.