我在 VBA 中有一個代碼,我一直用它來打開 Internet Explorer,復制特定網站上的資訊,然后粘貼到單元格中。現在的問題是該網站現在不再使用 IE。
我正在嘗試調整此代碼以將其與 Edge 和/或 Chrome 一起使用(我已經安裝了 Selenium),但實際上我正在為此苦苦掙扎。
有人可以幫我調整該代碼嗎?
Option Explicit
Sub Test()
Dim IE As Object
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Sheet3").Select
Range("A1:A1000") = "" ' erase previous data
Range("A1").Select
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "https://google.com" ' should work for any URL
Do Until .ReadyState = 4: DoEvents: Loop
End With
IE.ExecWB 17, 0 '// SelectAll
IE.ExecWB 12, 2 '// Copy selection
ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
Range("A1").Select
IE.Quit
Application.DisplayAlerts = True
End Sub
如何將 IE 提及切換到 Edge 或 Chrome 提及?比如把IE改成objEdge等等。
uj5u.com熱心網友回復:
您需要使用SeleniumBasic在 VBA 中自動化 Edge。SeleniumBasic 是一個基于 Selenium 的瀏覽器自動化框架,適用于 VB.Net、VBA 和 VBScript。
您可以按照以下步驟使用 SeleniumBasic 自動化 Edge 瀏覽器:
- 從此鏈接下載最新版本的 SeleniumBasic v2.0.9.0并安裝它。
- 從此鏈接下載相應版本的 Edge WebDriver 。
C:\Users\%username%\AppData\Local\SeleniumBasic在我的情況下找到SeleniumBasic的路徑(它也可能在這個路徑中C:\Program Files\SeleniumBasic),將Edge WebDriver復制msedgedriver.exe到這個路徑。- 重命名
msedgedriver.exe為edgedriver.exe. - 打開 Excel 并準備撰寫 VBA 代碼。
- 在VBA代碼界面,點擊Tools > References,添加Selenium Type Library參考,點擊OK保存。
- 示例 VBA 代碼(您可以根據需要進行更改):
Public Sub Selenium() Dim bot As New WebDriver bot.Start "edge", "https://www.google.com" bot.Get "/" bot.Wait 5000 bot.Quit End Sub
參考鏈接:從 Internet Explorer 轉換為 Edge 或 Chrome 瀏覽器的 VBA 腳本
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/382706.html
標籤:擅长 vba 谷歌浏览器 microsoft-edge
