通过硒VBA启动chrome或IE

时间:2015-08-22 14:16:34

标签: vba google-chrome internet-explorer selenium

我已经使用selenium VBA编写了一个用于Web数据下载的代码,它在Firefox中运行良好,但很多时候Firefox崩溃了。我试图从vba启动chrome / IE,但它没有正常发生。以下是我的代码....请帮忙。

Public Sub Untitled_2()

  Dim selenium As New SeleniumWrapper.WebDriver
  Dim By As New By, Assert As New Assert, Verify As New Verify, Waiter As New    Waiter

  driver.start "firefox", "https://indexes.nasdaqomx.com/Account/LogOn"

  'below 2 line don't work
  driver.start "ie", "https://indexes.nasdaqomx.com/Account/LogOn"
  driver.start "chrome", "https://indexes.nasdaqomx.com/Account/LogOn"


  selenium.setImplicitWait 10000
  selenium.Type "css=fieldset > div.editor-field > #UserName", "xxxxxxx"
  selenium.Type "css=fieldset > div.editor-field > #Password", "xxxxxxx"
  selenium.clickAndWait "css=fieldset > p > input.button.submit"
  selenium.Click "id=menu-5"
  selenium.Click "id=menu-1"
  selenium.clickAndWait "link=U.S."
  selenium.clickAndWait "id=NDX"
  selenium.clickAndWait "link=Weighting"
  selenium.Click "id=tradeDate"
  selenium.Click "link=20"
  selenium.Select "id=timeOfDay", "label=End of Day"
  selenium.Click "id=update"
  selenium.clickAndWait "id=exportLink"

  selenium.Stop

End Sub

错误截图如下:

Error screenshot

如何启动chrome或IE?

我的Chrome驱动程序路径是 C:\ Program Files(x86)\ SeleniumWrapper \ chromedriver.exe“

并且即驱动程序路径是 C:\ Program Files(x86)\ SeleniumWrapper \ IEDriverServer.exe“

3 个答案:

答案 0 :(得分:0)

@purnendumaity

我已经将Selenium Webdriver与VBA一起使用了一段时间。我在Windows XP,7或8上使用它的感觉是,它只适用于Chrome(并且不太好)。 Selenium论坛用户表示,最新的Firefox版本与Selenium崩溃。

嗯,Chrome有效。 Firefox崩溃和IE .....(=

我已经创建了一个默认宏来配置Chrome,然后再打开它。我相信诀窍是,始终打开默认网址,然后转到自己的网址:

Private Sub ConfigureChrome()
    'initiate maximazed
    selenium.addArgument "--start-maximized"
    selenium.setPreference "download.default_directory", Replace(ThisWorkbook.FullName, ThisWorkbook.name, "")
    selenium.setPreference "download.directory_upgrade", True
    selenium.setPreference "download.extensions_to_open", ""
    selenium.setPreference "download.prompt_for_download", False
    selenium.setPreference "--disable-popup-blocking", ""
    selenium.setPreference "--enable-panels", ""
    selenium.Start "chrome", "http://google.com"
End Sub

然后:

Private selenium As New SeleniumWrapper.WebDriver

Public Sub MyCode()
    Call ConfigureChrome    
    selenium.Open URL, 30000
    'blabalbalbal
End Sub

我希望它有所帮助

答案 1 :(得分:0)

我知道启动chrome后您需要selenium.Get "/",所以与IE可能相同。 您还已经将selenium声明为Web驱动程序,但是随后在代码中使用了driver,所以可以尝试以下操作:

Public Sub Untitled_2()

  Dim selenium As New SeleniumWrapper.WebDriver
  Dim By As New By, Assert As New Assert, Verify As New Verify, Waiter As New    Waiter

  'below 2 line don't work
  selenium.Start "ie", "https://indexes.nasdaqomx.com/Account/LogOn"
  selenium.Get "/"
  selenium.Start "chrome", "https://indexes.nasdaqomx.com/Account/LogOn"
  selenium.Get "/"

  selenium.setImplicitWait 10000
  selenium.Type "css=fieldset > div.editor-field > #UserName", "xxxxxxx"
  selenium.Type "css=fieldset > div.editor-field > #Password", "xxxxxxx"
  selenium.clickAndWait "css=fieldset > p > input.button.submit"
  selenium.Click "id=menu-5"
  selenium.Click "id=menu-1"
  selenium.clickAndWait "link=U.S."
  selenium.clickAndWait "id=NDX"
  selenium.clickAndWait "link=Weighting"
  selenium.Click "id=tradeDate"
  selenium.Click "link=20"
  selenium.Select "id=timeOfDay", "label=End of Day"
  selenium.Click "id=update"
  selenium.clickAndWait "id=exportLink"

  selenium.Stop

End Sub

答案 2 :(得分:-1)

这个简单的技巧是在chrome中打开你的URL而不是数据;

Sub test()
Dim selenium As New SeleniumWrapper.WebDriver
selenium.Start "chrome", "https://www.google.co.in"
selenium.Open ("https://www.google.co.in")
End Sub