如何使用Selenium在VBA用户窗体内嵌入网页?

huangapple go评论91阅读模式
英文:

How to embed a web page inside a VBA userform with Selenium?

问题

我有一个用户窗体,其中插入了一个VBA WebBrowser。我有一个工作的代码,调用这个浏览器,但由于IE已过时,我认为它会抛出错误。

我已经安装了Selenium Basic和webdriver.exe。我可以打开Edge,但打开后它不在用户窗体内,更不用说工作表中了。
如果有任何指导意见将不胜感激。以下是我的代码:

从工作表上的一个命令按钮调用宏"ShowBrowser"。

Sub ShowBrowser()
   '显示窗体。
    WebBrowser.Show vbModeless
End Sub

该窗体的名称为WebBrowser。从设计模式,插入了Microsoft Web浏览器。在属性窗口上,它被重命名为ObjWebBrowser。

从项目资源管理器,我打开窗体,点击查看代码,我有以下代码:

Private Sub ObjWebBrowser_Initialize()
     Dim selenium_Obj As New Selenium.EdgeDriver
     Call selenium_Obj.Start("edge", "https://google.com")
    selenium_Obj.Wait 5000
End Sub

我已经尝试了多种方法,但找不到在用户窗体内打开Edge的方法。

英文:

I have a userfrom inside of which a VBA WebBrowser was inserted. I have working code that calls this browser, but is throwing errors because, I think, of the deprecated IE.

I have installed Selenium Basic and webdriver.exe. I can open Edge, but when opened its not inside the user form, let alone anywhere in a worksheet.
Any pointers would be appreciated. Here is what I have:

From a cmdbutton on a worksheet Macro "ShowBrowser" is called.

Sub ShowBrowser()
   'Show the form.
    WebBrowser.Show vbModeless
End Sub

The form is named WebBrowser. From Design Mode, the Microsoft Web Browser is inserted. On the properties window, it is renamed to ObjWebBrowser.

From the Project Explorer, I open the form, click View Code, I have this:

Private Sub ObjWebBrowser_Initialize()
     Dim selenium_Obj As New Selenium.EdgeDriver
     Call selenium_Obj.Start("edge", "https://google.com")
    selenium_Obj.Wait 5000
End Sub

I have tried multiple approaches, but cannot find a way to open Edge inside the userform.

答案1

得分: 0

The reason why it's relatively simple to embed IE inside a form is indeed due to the control that is provided in the Microsoft Internet Controls Library (ieframe.dll).

图1:库位置

图2:控制位置

To do the same with Edge, you'd need a similar library that provides that control, but SeleniumBasic does not provide a userform control for that, it (only) provides an API to interact with a running instance of the web browser. This means that it needs to open the application in a separate window.

Hence, Selenium is not the tool that will allow you to embed a modern web browser inside a VBA userform.

In order to embed one inside a form, you'd need to make your own control which is no small task especially since Edge isn't based on the ActiveX technology that made most of what is done with IE possible.

Instead, Edge uses WebView2 and as discussed in another question, no one as yet created a fully functional library to bridge the gap between WebView2 and ActiveX. Sam Sirry's comment mentions that there was work done regarding this. You can have a look at the original VBForums post about it or a more recent post, if that's an option you want to look into. Note that I haven't tested those options.

Nonetheless, if I were you, I would reconsider the need to embed the browser inside the form and try to figure out if showing the browser as a separate window is acceptable or otherwise if it's possible to capture the inputs needed from the user inside the userform and then send those input via HTTP to an API or via selenium running a browser in the background.

英文:

The reason why it's relatively simple to embed IE inside a form is indeed due to the control that is provided in the Microsoft Internet Controls Library (ieframe.dll). (More details here)

如何使用Selenium在VBA用户窗体内嵌入网页?

Fig1: Library location

如何使用Selenium在VBA用户窗体内嵌入网页?

Fig2: Control location

To do the same with Edge, you'd need a similar library that provides that control, but SeleniumBasic does not provide a userform control for that, it (only) provides an API to interact with a running instance of the web browser. This means that it needs to open the application in a separate window.

Hence, Selenium is not the tool that will allow you to embed a modern web browser inside a VBA userform.

In order to embed one inside a form, you'd need to make your own control which is no small task especially since Edge isn't based on the ActiveX technology that made most of what is done with IE possible.

Instead, Edge uses WebView2 and as discussed in another question, no one as yet created a fully fonctional library to bridge the gap between WebView2 and ActiveX. Sam Sirry's comment mentions that there was work done regarding this. You can have a look at the original VBForums post about it or a more recent post, if that's an option you want to look into. Note that I haven't tested those options.

Nonetheless, if I were you, I would reconsider the need to embed the browser inside the form and try to figure out if showing the browser as a separate window is acceptable or otherwise if it's possible to capture the inputs needed from the user inside the userform and then send those input via HTTP to an API or via selenium running a browser in the background.

答案2

得分: 0

我无法找到在Excel用户窗体内打开Edge的方法。

这是因为Excel支持ActiveX对象,正如@DecimalTurn所示,Internet Explorer的引用是ieframe.dll(或shdocx.dll)。

您试图在Excel内部使用基于Chromium的Edge,这是行不通的。

也许旧版IE可以?试试看:

调用selenium_Obj.Start("ie", "https://google.com")
英文:

> I cannot find a way to open Edge inside the Excel userform

This is because Excel supports ActiveX objects and as @DecimalTurn showed the reference to Internet Explorer is ieframe.dll (or shdocx.dll).

You're trying to use Edge which is based on Chromium inside Excel and that isn't going to work.

Perhaps the old IE will? Give it a go:

Call selenium_Obj.Start("ie", "https://google.com")

huangapple
  • 本文由 发表于 2023年7月3日 06:42:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定