使用VBA自动化边缘模式(没有第三方软件)

发布于 2025-01-30 12:13:39 字数 2047 浏览 3 评论 0原文

当我一直在研究这个主题时,我正在创建一篇文章,并发现了一些矛盾的答案,该答案围绕着具有没有第三方应用程序的VBA自动化模式的能力(请参见下面的解决方案)。

我目前有一个在IE中完成以下操作的应用程序:

  • 验证IE的实例是否已经打开并导航到特定的URL;
  • 如果以上不是正确的话,可以创建一个新的IE实例;
  • 从HTML文档中获得可用的数据;然后
  • 单击元素以导航到其他选项卡和页面。

在IE贬值之前,我不确定如何指示代码与Edge IE模式专门通信,而不是尝试创建Internet Explorer的全新实例。 是否有人可能有我可以参考的特定示例


? 71994505/9791828“> https://stackoverflow.com/a/a/71994505/9791828

在该线程中,其中一个响应表明这种自动化是可能的。

截至2022年4月25日,您现在可以直接使用VBA自动化Edge模式,而无需任何其他第三方软件。我和我的同事通过与合作的Microsoft支持团队进行交流后,对以下指南进行了很好的测试。

您需要做什么

  1. 您的Windows版本必须至少为20H2。您可以在此处使用此指南检查Windows版本。

  2. 您的窗口需要安装以下KBS:KB5011487。或者简单地更新窗口,也应自动安装。您可以在此处使用本指南检查Windows更新历史记录。

  3. 最终在窗口上安装以下注册表键,然后重新启动:

Windows注册表编辑器版本5.00

[HKEY_LOCAL_MACHINE \ SOFTWARE \ PALICIES \ MICROSOFT \ Internet Explorer \ main]“ notifyDisableIeoptions” = dword:00000002

[HKEY_LOCAL_MACHINE \ SOFTWARE \ PALICIES \ MICROSOFT \ Internet Explorer \ Main \ EnterPriseMode] “ EnableGlobalWindowListinieMode” = DWORD:00000001

根据MS支持团队,上述方法应在2029年之前起作用。我相信可能很快就会发布有关此的官方文件。

在上述步骤之后,VBA将能够与Edge IE模式进行交互,就好像它是Internet Explorer窗口一样。您当前自动化InternetExplorer的代码。应用对象也将使用Edge IE模式。

响应2: https://lealen.microsoft.com/en-us/aswers/en-us/answers/questions/Questions/829365/vba -automation-with-gith-edge-ie-mode.html

社区专家之一说,没有硒和驾驶员,这是不可能的。

如果不是硒,我认为您不能与Edge IE模式自动化。

与Edge IE模式自动化的唯一方法是使用IEDRIVER。您不能只使用VBA做到这一点。您需要:

  1. 定义具有其他属性的Internet Exploroptions,该属性指向Microsoft Edge浏览器。

  2. 启动Internet ExplorerDriver的实例,然后通过Internet ExpplorerOptions。 IEDRIVER启动Microsoft Edge,然后以IE模式加载您的Web内容。

有关更多信息,您可以参考此文档

I'm creating a post as I've been researching this topic and have found some contradicting answers surrounding the ability to automate Edge IE Mode with VBA without third-party applications (see solutions below).

I currently have an app that accomplishes the following in IE:

  • Verifies whether or not an instance of IE is already open and navigated to a specific URL;
  • Creates a new IE instance if the above isn't true;
  • Scrapes data available from the HTML doc; and
  • Clicks elements to navigate to other tabs and pages.

Leading up to the deprecation of IE, I'm unsure how to direct my code to specifically communicate with Edge IE mode instead of it attempting to create a brand new instance of Internet Explorer. Does anyone possibly have specific examples that I could reference?


Response 1: https://stackoverflow.com/a/71994505/9791828

In that thread, one of the responses indicates that this automation is possible.

As of 25th April 2022, you can now directly automate Edge IE Mode with VBA without any additional third party-software. The below guidance has been well tested by me and my colleagues after obtaining it from exchanging with our partnered Microsoft Support team.

What You Need to Do

  1. Your Windows version needs to be at least 20H2. You can check your Windows version with this guide here.

  2. Your Windows needs to have the following KBs installed: KB5011487. Or simply update your Windows and this should be automatically installed as well. You can check your Windows update history with this guide here.

  3. Finally install the below registry keys on your Windows and restart:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet
Explorer\Main] "NotifyDisableIEOptions"=dword:00000002

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet
Explorer\Main\EnterpriseMode]
"EnableGlobalWindowListInIEMode"=dword:00000001

Per the MS Support team, the above method should work until 2029. Official documentation on this might be coming soon I believe.

After the above steps, VBA shall be able to interact with Edge IE Mode as if it is an Internet Explorer window. Your current code that automates the InternetExplorer.Application object will work with Edge IE mode as well.

Response 2: https://learn.microsoft.com/en-us/answers/questions/829365/vba-automation-with-edge-ie-mode.html

One of the community experts stated that this isn't possible without Selenium and a driver.

If Selenium is not an option, I think you can't automate with Edge IE mode.

The only way to automate with Edge IE mode is using IEDriver. You can't do it with only VBA. You need to:

  1. Define InternetExplorerOptions with additional properties that point to the Microsoft Edge browser.

  2. Start an instance of InternetExplorerDriver and pass it InternetExplorerOptions. IEDriver launches Microsoft Edge and then loads your web content in IE mode.

For more information, you can refer to this doc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰◇生如夏花灿烂 2025-02-06 12:13:40

不幸的是,到目前为止,使用VBA在边缘浏览器中实现IE模式自动化似乎是不可能的。即使使用SeleniumBasic,由于没有Internet ExpplorerOptions类别,因此您无法使用VBA在IE模式下运行自动化。

但是,目前,您不必担心IE退休后的IE自动化。响应1后,我试图禁用IE11浏览器,事实证明,VBA代码仍然成功启动了IE浏览器窗口。您可以预期您的VBA代码在IE退休后会起作用。

有关IE退休的更多信息,请阅读本文

我遇到了使用IE的应用程序的问题
自动化。这会解决吗? (更新:2022年4月14日)

作为IE 11申请退休的一部分,某些COM自动化
场景被无意中打破了。这些IE com对象已经
恢复到11月11日Windows的原始功能
2021“ C”更新和Windows 2022年3月10日“ B”更新。 com
IE11桌面之后,方案也将继续工作
申请在2022年6月15日之后被禁用。如果您继续
进行更新后经历问题,请联系App Assure
用于补救协助。

Unfortunately, it seems to be impossible to implement IE mode automation in the Edge browser using VBA so far. Even with SeleniumBasic, since there's no InternetExplorerOptions class or the like available, you cannot run the automation in the IE mode using VBA.

However, for now you don't have to worry about IE automation after the IE retirement. I tried to disable the IE11 browser following Response 1, and it turned out that VBA code still successfully launched IE browser window. You could expect your VBA code will work after the IE retirement.

For more information of IE retirement, please read this article:

I ran into issues with my application which utilizes IE through
automation. Will this be fixed? (Updated: April 14, 2022)

As part of the IE 11 application retirement, certain COM automation
scenarios were inadvertently broken. These IE COM objects have been
restored to their original functionality as of the Windows 11 November
2021 “C” update and the Windows 10 March 2022 “B” update. The COM
scenarios will also continue to work after the IE11 desktop
application is disabled after June 15, 2022. If you continue to
experience issues after taking the update, please contact App Assure
for remediation assistance.

智商已欠费 2025-02-06 12:13:40

我的VBA(v7.1.1126)直接从IE询问HTML;因此,我已经使用了带有Python(v3.10.6)的Selenium将等效数据从Chrome提取,并将其保存到文本文件中,然后我的VBA可以访问和解释。 VBA可以直接致电Python。

步骤1:安装最新的稳定[Python] [1]

步骤2:安装最新的稳定[Pycharm] [2],并从同一链接中阅读如何使用它

。单击右上方附近的三个点。单击设置,然后关于Chrome查看Chrome的版本。下载相应的驱动程序[此处。] [3]在Pycharm路径文件夹中保存驱动程序

步骤4:创建一个新项目并安装软件包Selenium和WebDriver-Manager,如PYCHARM链接上方所述,

步骤5:创建一个新的空白Python文件vba.py在下一步中由VBA更新此Willl。

步骤6:将此代码添加到新的VBA过程中,以确保它对您有效:

    Public Sub GetHtml(sURL$, sHTML$, iChannel%, sOutputFile$)
    
    'sURL - web page HTML code sought
    'sHTML - where HTML pulled is stored
    'iChannel - channel used for reading/writing output from/to textfiles
    'sOutputFile - where HTML is saved for manual interrogation when debgging e.g. \Output.txt
    
    Dim iLoopCount%
    Dim complete$, pythonexe$, script$, script2$, sFile_Path$, textline$
            
    'Python Script
    
    script = "" & _
    "from selenium import webdriver" & vbCrLf & _
    "from selenium.webdriver.chrome.service import Service" & vbCrLf & _
    "from webdriver_manager.chrome import ChromeDriverManager" & vbCrLf & _
    "from selenium.webdriver.chrome.options import Options" & vbCrLf & _
    "import time" & vbCrLf & _
    "outputFile = r""" & ActiveWorkbook.path & sOutputFile & """" & vbCrLf & _
    "completeFile = r""" & ActiveWorkbook.path & "\complete.txt""" & vbCrLf & _
    "options = Options()" & vbCrLf & _
    "options.headless = True" & vbCrLf & _
    "driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)" & vbCrLf & _
    "driver.get(""" & sURL & """)" & vbCrLf & _
    "driver.delete_all_cookies()" & vbCrLf & _
    "driver.implicitly_wait(12)" & vbCrLf & _
    "time.sleep(12)" & vbCrLf & _
    "pageSource = driver.page_source" & vbCrLf & _
    "driver.quit()" & vbCrLf & _
    "with open(outputFile, 'w', encoding='utf-8') as f:" & vbCrLf & _
    "    f.write(pageSource)" & vbCrLf & _
    "f.close()" & vbCrLf & _
    "with open(completeFile, 'w') as f2:" & vbCrLf & _
    "    f2.write('complete')" & vbCrLf & _
    "f2.close()"
    
    'Write the Script into a python file
    sFile_Path = "C:\Users\user\PycharmProjects\VbaProject1\Main\vba.py"
    Close #iChannel
    Open sFile_Path For Output As #iChannel
    Print #iChannel, script
    'closing channel forces complete write of script to python file
    Close #iChannel
    
    Close #iChannel
    Open ActiveWorkbook.path & "\complete.txt" For Output As #iChannel
    Print #iChannel, "incomplete"
    Close #iChannel
    
    pythonexe = "C:\Users\user\PycharmProjects\VbaProject1\venv\Scripts\python.exe"
    
    'restart point for python crash below
    complete = "incomplete"
    Do While complete = "incomplete"
        'Execute Script.py
        Call Shell(pythonexe & " " & sFile_Path)
        
        Application.Wait (Now + TimeValue("0:00:10"))
        
        'this loop minimises waiting time for the python run to complete
        
        iLoopCount = 1
        Do While complete = "incomplete" And iLoopCount < 7
            Application.Wait (Now + TimeValue("0:00:05"))
            
            'close textfile if python run not complete and file open
            Close #iChannel
            
            Open ActiveWorkbook.path & "\complete.txt" For Input As #iChannel
            Line Input #iChannel, complete
            
            'this is rarely true and if so it so far only happens for a python error
            ' which is resolved by running the python code from PyCharm in debug mode
            ' and then setting the next statement to the restart point above
            ' and resuming play of the VBA
            If complete = "incomplete" And iLoopCount > 5 Then Stop
            
            Close #iChannel
            iLoopCount = iLoopCount + 1
        Loop
    Loop
    
    Open ActiveWorkbook.path & sOutputFile For Input As #iChannel
    sHTML = ""
    Do Until EOF(iChannel)
        Line Input #iChannel, textline
        sHTML = sHTML & textline
    Loop
    
    End Sub

python代码将VBA插入vba.py上方,并带有其他评论:

''python

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import time
outputFile = r"C:\Users\user\Documents\HP Laptop\Documents\Documents\Jobs\DIT\IDMB\Output.txt"
completeFile = r"C:\Users\user\Documents\HP Laptop\Documents\Documents\Jobs\DIT\IDMB\complete.txt"
options = Options()
# headless means that browser requests solely pull the data without creating a GUI - a visual representation of a page
options.headless = True
# point driver to Chrome driver previously saved in PATH folder - Step 3
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
# request HTML page
driver.get("https://www.imdb.com/title/tt0097334/?ref_=adv_li_i")
driver.delete_all_cookies()
driver.implicitly_wait(12)
time.sleep(12)
# HTML
pageSource = driver.page_source
# close browser
driver.quit()

# save HTML for subsequent dissection by VBA
with open(outputFile, 'w', encoding='utf-8') as f:
    f.write(pageSource)
f.close()

# mark run as complete 
with open(completeFile, 'w') as f2:
    f2.write('complete')
f2.close()

  [1]: https://www.python.org/downloads/
  [2]: https://www.jetbrains.com/help/pycharm/installation-guide.html
  [3]: https://chromedriver.storage.googleapis.com/index.html

My VBA (v7.1.1126) interrogates the HTML from IE directly; so I have used Selenium with Python (v3.10.6) to pull the equivalent data from Chrome and save it to a text file which my VBA can then access and interpret. VBA can call Python directly.

Step 1: Install the latest stable [Python][1]

Step 2: Install the latest stable [PyCharm][2] and read how to use it from the same link

Step 3: Open Chrome. Click on three dots near top right. Click on Settings then About Chrome to see the version of your Chrome. Download the corresponding driver [here.][3] Save the driver in the PyCharm PATH folder

Step 4: Create a new project and install the packages selenium and webdriver-manager as explained in the PyCharm link above

Step 5: Create a new blank Python file vba.py This willl be updated by the VBA in the next step.

Step 6: Add this code to a new VBA procedure to ensure that it works for you:

    Public Sub GetHtml(sURL$, sHTML$, iChannel%, sOutputFile$)
    
    'sURL - web page HTML code sought
    'sHTML - where HTML pulled is stored
    'iChannel - channel used for reading/writing output from/to textfiles
    'sOutputFile - where HTML is saved for manual interrogation when debgging e.g. \Output.txt
    
    Dim iLoopCount%
    Dim complete$, pythonexe$, script$, script2$, sFile_Path$, textline$
            
    'Python Script
    
    script = "" & _
    "from selenium import webdriver" & vbCrLf & _
    "from selenium.webdriver.chrome.service import Service" & vbCrLf & _
    "from webdriver_manager.chrome import ChromeDriverManager" & vbCrLf & _
    "from selenium.webdriver.chrome.options import Options" & vbCrLf & _
    "import time" & vbCrLf & _
    "outputFile = r""" & ActiveWorkbook.path & sOutputFile & """" & vbCrLf & _
    "completeFile = r""" & ActiveWorkbook.path & "\complete.txt""" & vbCrLf & _
    "options = Options()" & vbCrLf & _
    "options.headless = True" & vbCrLf & _
    "driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)" & vbCrLf & _
    "driver.get(""" & sURL & """)" & vbCrLf & _
    "driver.delete_all_cookies()" & vbCrLf & _
    "driver.implicitly_wait(12)" & vbCrLf & _
    "time.sleep(12)" & vbCrLf & _
    "pageSource = driver.page_source" & vbCrLf & _
    "driver.quit()" & vbCrLf & _
    "with open(outputFile, 'w', encoding='utf-8') as f:" & vbCrLf & _
    "    f.write(pageSource)" & vbCrLf & _
    "f.close()" & vbCrLf & _
    "with open(completeFile, 'w') as f2:" & vbCrLf & _
    "    f2.write('complete')" & vbCrLf & _
    "f2.close()"
    
    'Write the Script into a python file
    sFile_Path = "C:\Users\user\PycharmProjects\VbaProject1\Main\vba.py"
    Close #iChannel
    Open sFile_Path For Output As #iChannel
    Print #iChannel, script
    'closing channel forces complete write of script to python file
    Close #iChannel
    
    Close #iChannel
    Open ActiveWorkbook.path & "\complete.txt" For Output As #iChannel
    Print #iChannel, "incomplete"
    Close #iChannel
    
    pythonexe = "C:\Users\user\PycharmProjects\VbaProject1\venv\Scripts\python.exe"
    
    'restart point for python crash below
    complete = "incomplete"
    Do While complete = "incomplete"
        'Execute Script.py
        Call Shell(pythonexe & " " & sFile_Path)
        
        Application.Wait (Now + TimeValue("0:00:10"))
        
        'this loop minimises waiting time for the python run to complete
        
        iLoopCount = 1
        Do While complete = "incomplete" And iLoopCount < 7
            Application.Wait (Now + TimeValue("0:00:05"))
            
            'close textfile if python run not complete and file open
            Close #iChannel
            
            Open ActiveWorkbook.path & "\complete.txt" For Input As #iChannel
            Line Input #iChannel, complete
            
            'this is rarely true and if so it so far only happens for a python error
            ' which is resolved by running the python code from PyCharm in debug mode
            ' and then setting the next statement to the restart point above
            ' and resuming play of the VBA
            If complete = "incomplete" And iLoopCount > 5 Then Stop
            
            Close #iChannel
            iLoopCount = iLoopCount + 1
        Loop
    Loop
    
    Open ActiveWorkbook.path & sOutputFile For Input As #iChannel
    sHTML = ""
    Do Until EOF(iChannel)
        Line Input #iChannel, textline
        sHTML = sHTML & textline
    Loop
    
    End Sub

Python code inserted by VBA into vba.py above with additional comments:

'''python

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import time
outputFile = r"C:\Users\user\Documents\HP Laptop\Documents\Documents\Jobs\DIT\IDMB\Output.txt"
completeFile = r"C:\Users\user\Documents\HP Laptop\Documents\Documents\Jobs\DIT\IDMB\complete.txt"
options = Options()
# headless means that browser requests solely pull the data without creating a GUI - a visual representation of a page
options.headless = True
# point driver to Chrome driver previously saved in PATH folder - Step 3
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
# request HTML page
driver.get("https://www.imdb.com/title/tt0097334/?ref_=adv_li_i")
driver.delete_all_cookies()
driver.implicitly_wait(12)
time.sleep(12)
# HTML
pageSource = driver.page_source
# close browser
driver.quit()

# save HTML for subsequent dissection by VBA
with open(outputFile, 'w', encoding='utf-8') as f:
    f.write(pageSource)
f.close()

# mark run as complete 
with open(completeFile, 'w') as f2:
    f2.write('complete')
f2.close()

  [1]: https://www.python.org/downloads/
  [2]: https://www.jetbrains.com/help/pycharm/installation-guide.html
  [3]: https://chromedriver.storage.googleapis.com/index.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文