使用VBA和Selenium自动化Google Chrome [英] Automate Google Chrome using VBA and Selenium
问题描述
我正在尝试使用Selenium加载项从Excel VBE中操纵Chrome.
I am trying to manipulate Chrome from the Excel VBE using the Selenium add-in.
- 我已经在参考文献中包括了硒类型库.
- 我正在运行Chrome v67.
- 我已替换了Selenium中的Chromedriver.exe文件 基本目录,其版本与Chrome v67兼容(即 Chromedriver版本38).
- I have included Selenium Type Library in my references.
- I am running Chrome v67.
- I have replaced the Chromedriver.exe file in the Selenium Basic directory with the version compatible with Chrome v67 (i.e. Chromedriver version 38).
我的VBA代码:
Public Sub seleniumtutorial()
Dim bot As New WebDriver
'bot.AddArgument "--headless"
'bot.AddArgument "--disable-extensions"
bot.Start "chrome", "http://google.com"
bot.Get "/"
bot.TakeScreenshot.SaveAs (ActiveWorkbook.Path + "/screenshot.jpg")
bot.Quit
End Sub
我收到以下错误消息:
如果我包含以下代码行,则不会出现错误消息,但是我看不到Chrome窗口:
If I include this line of code there is no error message but then I cannot see the Chrome window:
bot.AddArgument "--headless"
如果按照我对类似问题的答案中的建议,如果我包括这一行代码,那将无法解决问题.
If I include this line of code, as was suggested on SO answers to a similar question, it doesn't solve the issue.
bot.AddArgument "--disable-extensions"
推荐答案
从VBA的角度来看,您使用了错误的驱动程序类来自动执行Chrome.您已确认ChromeDriver的位置,但在您的代码中已将其完全忽略.
From a VBA perspective you are using the wrong driver class to automate Chrome. You confirmed the location of ChromeDriver but it is completely ignored in your code.
在此行中将WebDriver
替换为ChromeDriver
:Dim bot As New WebDriver
或使用:Dim bot As New Selenium.ChromeDriver
这是问题1.问题2是您收到的错误消息.使用WebDriver时,您应该会遇到运行时错误,而不是策略警告.
That's problem #1. Problem #2 is the error message you are getting. You should be getting a run-time error when you use WebDriver not a policy warning.
没有提供足够的信息来解决问题2,它可能与VBA没有任何关系.收到错误消息后,您可能希望与管理员聊天.
Not enough information provided to solve problem #2 and it may not have anything to do with VBA. Given the error message, you may want to have a chat with your admin.
这篇关于使用VBA和Selenium自动化Google Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!