launching chrome or IE via selenium VBA

流过昼夜 提交于 2020-01-23 04:08:16

问题


I have written a code for web data downloading using selenium VBA, it works good in Firefox, but many time Firefox is crashing. I tried to launch chrome/IE from vba but it's not properly happening. Below is my code....please help.

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 is as below:

How to launch chrome or IE ?

My chrome driver path is C:\Program Files (x86)\SeleniumWrapper\chromedriver.exe"

and ie driver path is C:\Program Files (x86)\SeleniumWrapper\IEDriverServer.exe"


回答1:


@purnendumaity

I've been using Selenium Webdriver with VBA for a while. My feeling using it on Windows XP, 7 or 8 is, it only works well (and not very well) with Chrome. Selenium forum users have said that the most recent Firefox versions crashes with Selenium.

Well, Chrome works. Firefox crashes and IE..... (=

I've created a default macro to configure Chrome before open it. I believe that the trick is, always open a default url and then, go to your own:

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

And then:

Private selenium As New SeleniumWrapper.WebDriver

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

I hope it helps




回答2:


I know you need selenium.Get "/" after launching chrome, so it may be the same with IE. You've also declared selenium as a web driver, but then used driver in your code, so maybe try the below instead:

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



回答3:


This simple trick to make open your URL in the chrome instead of data;

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


来源:https://stackoverflow.com/questions/32157107/launching-chrome-or-ie-via-selenium-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!