When it comes to browser automation tasks in Excel VBA like filling a web form, login to a website etc. most of them use Internet explorer by adding a reference to Microsoft Internet controls.
VBA users have been using IE automation from many years and most of them still use it. but there is a problem with Internet explorer! As we all know Internet explorer is the ‘God of all browsers’ – I mean we just use it to download other browsers because of its performance issues. Due to its poor performance automation also would be slow.
Then why not use high performance browsers like chrome or firefox? Yes this post is all about browser automation in VBA using selenium VBA wrapper which enables automating high performance browsers like chrome and firefox with just few lines of code.
What is Selenium
Selenium is a testing framework. Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.
Some good guy has written a .net wrapper for selenium which can be used in VBA and VB.net and that is the wrapper we are going to use in this tutorial.
Prerequisites :
- Basic knowledge of VBA
- Microsoft Excel installed in windows OS – Selenium VBA wrapper is a windows COM library so it works only on windows.
- Selenium VBA wrapper library – you can download and install it from here
Getting started with selenium VBA
1.Open any excel file, go to code editor (Alt + f11) and create a sub.
Public Sub seleniumtutorial() End Sub
2.Add selenium VBA reference
go to tools > references > check ‘Selenium Type Library’ and click ok.
3.Define and start web driver
selenium web driver is what controls the browser.
syntax : Dim drivername as new WebDriver
Dim bot as new WebDriver
This driver is used to start and automate the browser.
4.Start browser
A browser can be started by using start method of webdriver.
syntax : driver.Start "browsername", "baseurl"
currently supports chrome, firefox, opera, PhantomJS & IE
bot.Start "chrome", "http://google.com"
Simple example
Lets say we have to open a website and take screenshot of it. This can be done in just 2 lines of code. open website using Get
method and screenshot using TakeScreenshot
method
Public Sub seleniumtutorial() Dim bot As New WebDriver bot.Start "chrome", "http://google.com" bot.Get "/" bot.TakeScreenshot.SaveAs (ActiveWorkbook.Path + "/screenshot.jpg") bot.Quit 'quit method for closing browser instance. End Sub
Above code is self explanatory, when you run this code a screenshot of website is saved in the folder in which current excel file is present.
Too easy right! But who wants a screenshot of google? Ah yes, this simple example is used just to explain the steps in automation. I’ll be explaining few practical situations of automation later in this tutorial.
Adding more functionality
Lets say I’m an SEO analyst, I want to search few words on google and take screenshot of results. List of keywords are in column A of keywords sheet.
Lets start by adding a sub
Public Sub keywordsearch() End Sub
1.Define WebDriver
and a Range
to get range of keywords.
Dim bot As New WebDriver, rng As Range
2.set range to list of cells in column A
Set rng = Range(Worksheets("keywords").Range("A2"), Worksheets("keywords").Range("A2").End(xlDown))
Above code makes setting a range dynamic because of End(xlDown)
End(xlDown) = next non empty cell in that row (the cell you reach when you press ctrl + down in excel)
3.Start web driver
bot.Start "chrome", "http://google.com" bot.Window.Maximize
4.Loop through each cell in range, search keyword in google
For Each cell In rng bot.Get "/" bot.FindElementByName("q").SendKeys (cell.Value) bot.FindElementByName("btnG").Click bot.Wait 1000 bot.TakeScreenshot.SaveAs (ActiveWorkbook.Path + "/screenshot_" + cell.Value + ".jpg") Next bot.quit
In the above code, First we use For
to loop through each cell then get ‘/’ which is homepage, if we need image search then ‘/images’ is used. After loading homepage, find input textbox to enter the search word.
Selenium has many methods to find a html element, Most commonly used methods to find elements are FindElementById
, FindElementByName
, FindElementByXpath
To find a HTML element, we need a selector. selector may be ID or name or Xpath or class or tag. If we have ID then FindElementById
is used, FindElementByName
is used if we have name of the element and so on. Finding a selector is simple, just go that webpage > right click on the element for which we need a selector > Inspect element
. Prefer ID first, if there is no ID or name then copy xpath by right clicking again on the element.
As you can see in the image above, input textbox on google homepage has name attribute so we can use FindElementByName
to find element and SendKeys
method to type the keyword in textbox.
Button also has a name attribute with value “btnG” so FindElementByName
is used again to find the search button and click it using Click
method.
Wait for 1 second for search to complete and then take a screenshot using TakeScreenshot
method , moving to next cell and finally quit driver using Quit
method.
Noteworthy methods :
- All FindElement methods
ExecuteScript
– used to run any custom Javascript code.IsElementPresent
(Removed now)PageSource
refresh
until
Proxy
Window
These are the commonly used methods in selenium, give it a try.
Wait for element in Selenium VBA
In previous versions of Selenium wrapper, There were few methods like IsElementPresent
and WaitForElementPresent
which were used to check if element is present, wait till element is available. But these methods are removed now. So How do we wait for elements?
Current methods which are used to identify elements such as FindElementById
, FindElementByXPath
have an implicit wait of 3 seconds which means – If the element was not found initially then these methods wait for up to 3 seconds to see element becomes available.
If you just want to wait till element becomes available then just increase the implicit wait time of these methods using :
driver.FindElementById("someId", timeout:=10000) 'waits for up to 10 seconds driver.FindElementById("someId", timeout:=20000) 'waits for up to 20 seconds
Check is element is present in Selenium VBA
If your goal is to just check if element is present or not instead of waiting for the element then it can be done using :
Set myelement = bot.FindElementById("someId", timeout:=0, Raise:=False) If myelement Is Nothing Then MsgBox ("no element found") else myelement.Click End If
My Practical use cases of selenium VBA
I had an excel list in my office which contains usernames and passwords of around 200 clients which are used to login to a web admin panel. whenever I need some information, I had to login manually by copying the username & password of that particular client and pasting in the browser and login. So I just wrote some code for autologin, used a shortcut key for that sub. whenever I wanted to login, I just click on the client name and used my shortcut key!
Fetching youtube channel subscribers count. This can be done using XML HTTP requests also. comment below if you want to know more about fetching data using XML HTTP.
Automating product purchases, few other automations which need browser involvement exclusively.
Update : Checkout next part of this tutorial Scrape website using Selenium VBA to learn how to scrape website data using selenium VBA and best practises of scraping data in excel.
Wrapping up
This was a quick tutorial, there is lot more that can be done with selenium VBA. you can download official examples file here which contains lot of examples on Selenium VBA automations. have a look at them and start automating!
If you have any questions or feedback, please comment below.
- Flutter lookup failed in @fields error (solved) - July 14, 2023
- Free open source alternative to Notion along with AI - July 13, 2023
- Threads API for developers for programmatic access - July 12, 2023
Hi Ranjith,
I was using IE for browser automation.
Can you help me doing a little conversion of my IE code for selenium so it clears me.
Is it possible I can send you an excel file via email with code?
Many thanks for your help
Regards
yes sure, you can mail me the excel file to [email protected]
How to get the web results in the excel at the same time.
check out the new post on website data scraping for that https://codingislove.com/scrape-website-using-selenium-vba/
Nice article, well described. I have been searching the web for an info closely related to this. I want to use VB.net to design a nice GUI of which the back end is to automate some mundane website tasks, seeing that it is possible to use selenium with VBA, I believe it should be possible to do with VB.net also, do you have an idea how I can go about this? Thanks in advance.
Hello Mukhtar,
Yes that can be done, Previously I have made few bots in vb.net using selenium. Best way to get started is using nuget package available here : https://www.nuget.org/packages/Selenium.WebDriver/2.53.0
If you are not familiar with using package manager then download Selenium web driver for .Net here : http://www.seleniumhq.org/download/
You can find it under C# language which is the same for VB.net too. Then add a reference to webdriver.dll and Import appropriate NameSpaces you need like
Imports OpenQA.Selenium.Firefox
Imports OpenQA.Selenium.Chrome
By default, only firefox works without any additional driver. For chrome and other browsers you have to download appropriate drivers from the same link above. Hope that helps.
Thanks Ranjith,
I have realised where I was getting it wrong from your response. I didn’t find selenium download for vb.net before not knowing the same one for C# can be used for vb.net.
How to get the output in excel file. Code:
Public Sub search()
Dim bot As New WebDriver, rng As Range
Set rng = Range(Worksheets(“keywords”).Range(“A2”), Worksheets(“keywords”).Range(“A2”).End(xlDown))
bot.Start “chrome”, “http://electoralservicessearch.azurewebsites.net/searchbyname.aspx”
bot.Window.Maximize
For Each Cell In rng
bot.Get “/”
bot.FindElementByName(“txt_Epicno”).SendKeys (Cell.Value)
bot.FindElementByName(“btn_Epic”).Click
bot.Wait 5000
Next
bot.Quit
End Sub
I need your help.
Can you send me sample data? What are you searching for in that webpage?
I need to get the details from the tamilnadu election website http://www.elections.tn.gov.in. by clicking searchvoters and entering the voters epicno for ex : MSS3007457 it gives the all details of voters id . i need to save the details in excel format.all i need is the name,part no , slno and the voters constituency. I’ve voters epic number for checking in excel and i can only see the details in screen by running the macro which i learned from your site. The direct link is
“http://electoralservicessearch.azurewebsites.net/searchbyname.aspx”. can you help me in this regard.
Hi, here’s code for scraping data of one voter :
Public Sub scrapevoters()
Dim bot As New WebDriver
bot.Start “chrome”, “http://electoralservicessearch.azurewebsites.net”
bot.Get “/searchbyname.aspx”
bot.FindElementById(“txt_Epicno”).SendKeys (“MSS3007457”)
bot.FindElementById(“btn_Epic”).Click
Sheets(1).Cells(1, 1).Value = bot.FindElementById(“lbnameen”).Text ‘Name
Sheets(1).Cells(1, 2).Value = bot.FindElementById(“lbparten”).Text ‘Part number
Sheets(1).Cells(1, 3).Value = bot.FindElementById(“lbserialen”).Text ‘Serial Number
Sheets(1).Cells(1, 4).Value = bot.FindElementById(“lbpartnameen”).Text ‘Polling station
End Sub
Use it in a For loop to scrape multiple voters.
bot.wait is not required here as it waits for page load automatically after click. Hope it helps.
I’m able to loop through the following codes:
Public Sub scrapevoters()
Dim bot As New WebDriver, rng As Range, i As Integer
Set rng = Range(Worksheets(“keywords”).Range(“A2”), Worksheets(“keywords”).Range(“A2”).End(xlDown))
bot.Start “chrome”, “http://electoralservicessearch.azurewebsites.net/searchbyname.aspx”
bot.Window.Maximize
For Each Cell In rng
bot.Get “/”
bot.FindElementByName(“txt_Epicno”).SendKeys (Cell.Value)
bot.FindElementByName(“btn_Epic”).Click
i = i + 1
Sheets(2).Cells(i, 2).Value = bot.FindElementById(“lbepicen”).Text
Sheets(2).Cells(i, 3).Value = bot.FindElementById(“lbnameen”).Text
Sheets(2).Cells(i, 4).Value = bot.FindElementById(“lbacnameen”).Text
Sheets(2).Cells(i, 5).Value = bot.FindElementById(“lbparten”).Text
Sheets(2).Cells(i, 6).Value = bot.FindElementById(“lbserialen”).Text
Sheets(2).Cells(i, 7).Value = bot.FindElementById(“lbaden”).Text
Next
bot.Quit
End Sub
It works. Now here is the situation when the voters id is wrong then the page shows “Your Record is Not Available Now. For Further Enquiry Please Call 1950”. After the message looping stops. If the page doesnot display the records then it should go and fetch the next record. How to do it. sample voters list in my excel sheet BDZ2630614,BKH2950970,DDX2789360 and so on. the second record BKH2950970 gives “Your Record is Not Available Now. For Further Enquiry Please Call 1950”. how to loop through next record. Thanks
Try this code
Sub Test()
Dim bot As New WebDriver
Dim r As Long
Dim c As Long
bot.Start "chrome", "http://electoralservicessearch.azurewebsites.net"
bot.Get "/searchbyname.aspx"
For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
With Sheets(1)
c = 1
bot.FindElementById("txt_Epicno").SendKeys (CStr(.Cells(r, 1).Value))
bot.FindElementById("btn_Epic").Click
Next r
End Sub
Hi satyanarayanan,
The URL is “https://electoralsearch.in/” and then the place where we type the EPIC No, there you have the Catcha Code, how did you get past that ?
“http://electoralservicessearch.azurewebsites.net/searchbyname.aspx”
This above link can’t be accessed.
And also ,
There is no
http://www.elections.tn.gov.in. by clicking “searchvoters” , this way links to “https://electoralsearch.in/ ” this link only.
hi, selenium is opening browser but not navigating to url in vba, i read many articles and spent hours but nothing worked, will appreciate ur help
Hi Ravi, Is it showing any error?
Hi,
it doesnt show any error, it simply opens chrome/firefox and
that’s it , doesn’t do anything ahead
I think you are just missing the http:// in URL. Are you using the same code as shown above?
Hi, let me also tell you i am on win 7 64 bit, office 2010
Hi , Ranjith thanks for your reply
This is what is see in the url (data:,) when chrome opens,
I tried your above code also
Public Sub scrapevoters()
Dim bot As New WebDriver
bot.Start “chrome”, “http://electoralservicessearch.azurewebsites.net”
bot.Get “/searchbyname.aspx”
end sub
but again there is no navigation.
Can you please share the link from where i can download selenium wrapper or selenium basic
(let me tell you i am new to vba)
You can download the latest version of selenium wrapper from here – https://github.com/florentbr/SeleniumBasic/releases
Ranjith kumar, thanks for your replies, after a lot of tries i found that for me its only working when i downgrade firefox to version 39.0 , its fine anyways , its working all fine but firefox closes by itself after some seconds.
Following codes inserted completes the looping:
On Error Resume Next
If Err.Number <> 0 Then
Err.Clear
Sheets(2).Cells(i, 2).Value = 0
End If
On Error GoTo 0
But is there any other way to the solve the looping. Thanks
Hi, The best way to handle this issue is to use IsElementPresent method on name element, check if element is present, then use goto next to continue loop if name element is not present.
Name element would be present in every voter for sure and also rendered server side if epic number is found so using IsElementPresent method is the right way in this case but this method is buggy in the latest version of Selenium wrapper. So, Use the basic error handling that you mentioned above as a workaround for now. I would update you when IsElementPresent method is working fine.
Hi,
How to loop through select option and scrape data by each select options using selenium VBA .
Thanks.
Hello, You can do it by getting the select element first, then get option elements in that element using getElementsByTag and loop through them. Example code here – http://pastebin.com/j67yRvH1
Hi,
Thanks for the Example.
I ran the following and received this error. The Edge browser window opened but did not navigate to google.com.
Public Sub seleniumtutorial()
Dim bot As New WebDriver
bot.Start “edge”, “http://google.com”
End Sub
Error received: Run-time error ‘6’: NoSuchDriverError A session is either terminated or not started.
Hi Eric, This seems to be a known bug – https://github.com/florentbr/SeleniumBasic/issues/87
Manually updating edge driver should fix it, instructions in the link. Also try adding bot.get “/” in the next line and see if it works.
Solved by updating the Edge driver:
Copy “C:\Program Files (x86)\Microsoft Web Driver\MicrosoftWebDriver.exe”
Rename it to edgedriver.exe
Replace it in %LOCALAPPDATA%\SeleniumBasic
glad it worked!
Hi Ranjith, great article! I just found out about Selenium a few days ago and have been struggling to find an article with some well-explained examples of how to use Selenium. Do you mind if I email you about what I’m trying to accomplish using VBA and Selenium? I’m decent at VBA.
Sure, you can mail me at [email protected]
I’m trying to use selenium to automate some work in Chrome. When I try to open chrome using the code below I get an error in chrome that says “You are using an unsupported command-line flag: –ignore-certificate-errors. Stability and security will suffer.”
Code:
Sub OpenChrome()
Dim selenium As New WebDriver
selenium.Start “chrome”, “http://www.google.com”
selenium.setTimeout (“120000”)
selenium.setImplicitWait (5000)
End Sub
I’ve built macros with VBA that automate work in internet explorer before, but I haven’t done it for chrome. It sounds like using selenium is the best way to build macros for navigating through chrome and interacting with the web. What am I doing wrong to get this error?
Hi Todd, This is a known issue – https://bugs.chromium.org/p/chromedriver/issues/detail?id=799
Just update your chrome browser and make sure that you have the latest version of Selenium wrapper – https://github.com/florentbr/SeleniumBasic/releases and it should be gone!
Updated Selenium wrapper and now I’m getting this error:
“Run-time error ’33’:
SessionNotCreatedError
session not created exception
from unknown error: Runtime.executionContextCreated has invalid
‘context’:
{“auxData”:{“frameId”:”2392.1″,”isDefault”:true},”id”:1,”name”:””,”orig
(Session info: chrome=54.0.2840.99)”
Does this error look familiar?
Okay, I tested it out with the latest version of selenium wrapper and got the same error. So I looked into the chrome driver version and found out that it’s v 2.21while the latest version with bug fixes for chrome 54 is v 2.25.
Selenium wrapper installer comes with v 2.21 so we have to manually update the driver.
First download chrome driver 2.25 from here – https://sites.google.com/a/chromium.org/chromedriver/downloads then go to your selenium path – C:\Users\username\AppData\Local\SeleniumBasic and replace the chrome driver with the latest one and it should work fine.
I’ll make a request to update the chrome driver in selenium wrapper installer. Meanwhile, manual updating is the way to go.
The beauty of development is – We got to stay ahead of everyone 🙂
Hi Ranjith,
How do I use a conditional statement like below to find if an element is not present
If driver.WaitNotElement(By.ID(“loadMore”))
I get a “Type Mismatch Error”
Hi Salam,
If you are trying to check if Element is present or not then this code should do the work.
Set mybutton = bot.FindElementById(“someid”, timeout:=0, Raise:=False)
If mybutton Is Nothing Then
MsgBox (“no element found”)
End If
But if you are trying to wait till the element becomes available the check the updated article. I’ve added a new section – Wait for element in Selenium VBA.
I am very happy that i found your site finally. This site is awesome and very informative about different stuffs on complicated topics about which thousand others barely dare to deal with. Anyways, i got an issue in my written code. Gonna paste it underneath. Something is wrong with this. Every time i run it, i get an error showing “run time error 13, type mismatch”. By the way, i am running this code into my excel VBA editor.
Here is the code: https://bin.codingislove.com/eridenoxig.vbs
Any help would be greatly appreciated. Thanks in advance.
I have already found the error and that is “driver.FindElementByClass(“info”)”, whereas the proper calling should be “driver.FindElementsByClass(“info”)”. By the way, can i run the code using seleniumwrapper.webdriver instead of webdriver.
Hi Shahin, There are few mistakes in your code. Use
FindElementsByClass
instead ofFindElementByClass
in set posts line because there are multiple elements. Define posts and post as object instead of webelements.Here’s the working code – http://pastebin.com/tLb6HUe1
Seems like you are using an old version of Selenium VBA which has OPEN method. Install latest version and use GET method. Phantomjs is kind of unstable. It works sometimes, doesn’t work sometimes due to timeout issues. Use Chrome instead, It works perfectly. Also, try HTTP request method which I’ve used here – https://codingislove.com/parse-html-in-excel-vba/ It will be 20 times faster than selenium 🙂
Thanks, sir. I’m really indebted to you for your kind reply. I am being able to write this code using selenium because of your blog. Thanks again.
Glad it’s helping you and you can call me Ranjith 🙂
Thank you so much Ranjith, this article and other stuff in coding is love are helping me so much in enriching data for marketing purposes. Thank you, thank you, thank you!!
Hello Claudio, Glad its helping you 🙂
Hi Ranjith,
Can you please help?
I got an updated Selenium as per your advised above but got an error on
data:, in Chrome. In VBA, Runtime error ‘0’ : webrequesterror.
Thanks
Junnel
Hi Junnel, Make sure to have the latest chrome browser and also latest chrome driver. Read this discussion – https://codingislove.com/browser-automation-in-excel-selenium/#comment-518
Let me know if that helps.
This seems to be a great tutorial but I failed to begin because of error at the very first line.
bot.Start "chrome", "http://google.com"
gives me `Run-time error 0, WebRequestError,
when I executing :
bot.Start "firefox"
i get:
`gives me Run-time error 21, TimeoutError, Firefox failed to open listening port 127.0.0.1:11423 within 15.s. Do you know how to deal with it?
Hi Peter,
The solution for this runtime error is available at https://forum.codingislove.com/topic/9/unable-to-add-reference-for-selenium-type-library/3 Have a look
Hi ,
Great beginner tutorial to get me started, thanks! I’m wondering though; can I late bind with selenium:
I would like to create a macro that runs without library references. For IE that was just a case of swithcing
Dim bot As New InternetExplorer
toDim bot As Object: Set bot = CreateObject("InternetExplorer.Application")
and leaving everything else the same
How do I do this for Selenium?
Dim bot As Object: Set bot = CreateObject("Selenium.WebDriver")
gives a run time error for ie, and an unknown error for edge. Is there something else I should try?Im getting automation error while using this code
Public Sub seleniumtutorial()
Dim bot As New WebDriver
bot.Start “chrome”, “http://google.com”
bot.Get “/”
bot.TakeScreenshot.SaveAs (ActiveWorkbook.Path + “/screenshot.jpg”)
bot.Quit
‘quit method for closing browser instance.
End Sub
I have selenium wrapper installed and used it with latest chrome driver. am i missing anything?
It says runtime error(xxxxxxxxx)
Automation error
Hi Ashok, It is a known issue. Make sure that you have .Net framework 3.5 installed.
Thanks a lot, wow, didnt know it was that easy, but now i have another problem, there is no id in the webpage im trying to scrape, all are in class name it looks like this
GARDENA CANADACOR
GARDENA CANADA is the string im trying to extract, but the span calss sc-attribute is same for all attributes, the attribute name is sc-grid-cell sc-grid-cell-brand but when i tried to extract the value it says some error as compound xxxxx. seems like the space between the attribute is problem, any help is greatly appreciated.
Just use Xpath as explained in the article.
sent you an email with the snippet as im unable to attach the same her in the comment, subject would be: Gardena WEbscraping-Ashok
Please assist ..
These examples show how to start the browser…is there a way to capture an already existing instance of a browser for manipulation? Thanks.
Hi John, Unfortunately, there’s no way to control an existing browser instance which was opened manually 🙁
Thanks a lot for this useful article
I have installed Selenium the latest version and when trying the first code .. I got chrome crashed (Chrome is the latest version too)
Any idea?
Try updating chrome driver as explained in – https://forum.codingislove.com/topic/9/unable-to-add-reference-for-selenium-type-library/3
I already found this link but I couldn’t find “SeleniumBasic” folder in Local. That’s weird
I found this folder “SeleniumBasic” in Program Files and I copied the “chromedriver.exe” and replaced the old one .
Then I tried this code
Public Sub seleniumtutorial()
‘References : Selenium Type Library
‘———————————-
Dim bot As New WebDriver
bot.Start "chrome", "http://google.com"
End Sub
The google chrome appeared for a second then disappeared. Any idea?
The Browser will quit once it completes execution of code. Write more code or use bot.sleep or application.wait depending on your context.
Thank you very much for your patience
I have added this line : bt.Sleep
and I encountered “chromedriver.exe has stopped working” and there is a warning message of “Disable Developer mode extensions”
I am using the latest version of chrome (Version 61.0.3163.91 (Official Build) (32-bit)) and the latest version of selenium (SeleniumBasic-2.0.9.0)
I have solved this by copying “chromedriver.exe” in the lnik you attached to the “SeleniumBasic” folder and now no errors but I got in the address bar “data:,” and there’s a hint “Chrome is being controlled by automated test software”…
Regards
have you fix this issue please tell me
It is solved partially .. Now I could test some codes and they are working
Hi Ranjith,
Could you please let me know how to hit keyboard keys, ex I want to press enter Key
Hi Ravi, You can use
SendKeys
method to send keystrokes. Ex: bot.FindElementById(“identifier”).SendKeys(keys.Enter)Is there another way to type a text in inputbox instead of using SendKeys ?
As far as I know, SendKeys is the only way in selenium.
Thank you very much for reply ..
Is it working even if the desired page not shown?
Another question : is there a way to make the driver hidden such as PhantonJSDriver?
If the element is hidden then selenium cannot find it. If the element is not hidden and the browser is just headless then selenium can still sendKeys. You can use phantomJS itself instead of chrome.
Thank you very much for quick reply
Last question : how to make the desired tab to be the active tab .. all the time while running the code using selenium?
Hello, great post. I have some question… How can I set one or more firefox profile? I have seems code for java but not to vba. Thanks!!
Hi. I solved my previous question. However, the browser does not load the page I entered and send me this:
Run-time error ’21’:
timeouterror
firefox failed to open the listening port 127.0.0.1:57348 whithing 15s
I have read that it is necessary to use geckodriver for firefox version 48 and higher. I have already downloaded it but I do not know how to use it through VBA.
Did you ever find out how to get geckodriver working with later versions of firefox, I’m trying to do this now and am stuck with how to proceed!
Hi, can you help. I have played with Selenium for a good while and had to get a new PC. Installing Selenium went fine but it just reports automation error at the first step ‘driver.Start “chrome”, “http://google.com”. The same code still works on my old PC.
Are there other bits of MS software that need to be installed that might have already been on my previous PC?
Hello. How can I press ctrl+shift J in chrome.?
I used this for firefox and works:
driver.FindElementById(“my-id”).SendKeys Keys.Control + Keys.Shift, “k”
for chrome I must replace k with j to open console.
To evaluate the operation of the keys I tried keys.control, “v” and works but the use of keys.control + keys.shift, “j” or only keys.F12 does not work for me in chrome, however in firefox yes.
Could you please let me know if I can register the DLLs manually and not
install the setup on my client systems. I would like to know the DLL list which
i need to register as well as how i can register them as I do not have access
to install this setup on client systems.
Please help
It should be possible but it’s a long process!
Hello,
I’m getting Rub Time Error = 21
TimeoutError
Firefox failed to open the listening port 127.0.0.1:57590 within 15s
Hi Kamal,
Have you managed to find a way to overcome the Firefox error with listening port opening failure?
Dear Ranjith,
this is already a good explanation and the bunch of information on the selenium web driver. thank you again.
I want to study the whole “objects” of the selenium driver. Like,
in bot.get(“/”) statement,
we understand the working of .get object.
so selenium web driver may have consisted of many objects which can be useful for any task.
can you please tell me, where we can learn all?
Hi Kamal, You can use VBA autocomplete to view all the available methods and properties. Type
bot.
and VBA will show you all the methods available. The syntax of the methods will be shown once you type the method name with a bracket. Ex:bot.get(
Hi all, these Many days I was using selenium wrapper and it worked fine. But today I faced issue with the Chrome driver. I am not able to type data in a field and click any buttons. I didn’t receive any error, the page opened in chrome was in the same page. What is the solution for this? Why it suddenly not working? Please give me some solution.
Hi Gayathri, Make sure to update the chrome driver manually. Read this discussion – https://codingislove.com/browser-automation-in-excel-selenium/#comment-519
Hi Ranjith,
After I lernt and use json tutorial from you, I started with selenium now. I started with Simple Example provided by you.
I have received an error, the details are as follow:
Chrome: Version 65.0.3325.181 (Official Build) (64-bit)
Error on line: bot.Start “chrome”, “http://google.com”
Error in VBA: WebRequestError The underlying connection was closed. An unexpeted error occurred on a receive.
Note: It opens new chrome window and alert displayed regarding “Disable developer mode extensions” with “Disable” and “Cancel” buttons.
Refferances added in VBA:
Visual Basic For Applications
Microsoft Excel 16.0 Object Library
Microsoft Office 16.0 Object Library
OLE Automation
Selenium Type Library (downloaded from the github link you mentioned)
Please guide further.
Thanks in advance,
Jigar B
Hi Jigar, Make sure to update the chrome driver manually. Read this discussion – https://codingislove.com/browser-automation-in-excel-selenium/#comment-519
Hi Ranjith,
I have been using IE for browser automation with MS Access VBA.
For various reasons, I need to convert one sub to Chrome and I am having a heck of a time getting it figured out.
Can you help me doing a little conversion of my IE code for selenium.
I can send you my current code and the source code for the pages I am working with (it is a secure site, so I can’t give you the login).
Thank you in advance for your help!
Hi Renee, share your current selenium code.
Hi Ranjith,
I’m facing a tough problem, need your suggestions, I just make a try on a automation with selenium & excel vba. My target url is designed to generate a new pop up from the just beginning , I need to type username and password on the newly pop up window, and login to select some menu to download files.
My codes can open Firefox with the url, but cannot recognize the element by all kinds of methods (I can see the element’s id and name by right click on mouse choosing inspect the element )in the login window, I guess my codes even cannot recognize the new window. Because after pop up I use “windows.count” the feedback is 1. Is there method to recognize the new window, and then take use of its elements to type in. Thanks sincerely!
Hi Sunyoung
Did you ever figure this out? I have this same issue and I need to know how to get to the new window. Thanks
I keep getting runtime error 11 element not visible for line:
bot.FindElementByName(“btnG”).Click
The btn clearly is visible because the line before completes in terms of inputting the demonstration value with:
bot.FindElementByName(“q”).SendKeys (currCell.Value)
I introduced waits but that didn’t seem to make a difference.
How do I resolve this please? If a loop is required please could you show how it would be applied to your demo example.
Thanks
I noticed it was a submit button so I changed the event to .Submit not .Click and it works fine.
Hi Ranjith,
I’m new in web scrapping world using VBA, your post is very useful, thank you.
I would like to avoid Firefox browser or Opera Browser (I use them mostly) load images. In web there are post for this but using Python, I have tried to write a script for VBA, but it does not work. If you have information I would like to know about how webdriver profiles and preferences works in VBA.
I have used this:
Private bot As Selenium.WebDriver
Private Sub CommandButton1_Click()
Set bot = New Selenium.WebDriver
bot.SetPreference "permissions.default.image", 2
bot.Start "firefox", "https://codingislove.com/browser-automation-in-excel-selenium/"
bot.Get "/"
bot.Window.Maximize
End Sub
But, as I mentioned it does not work, I mean Firefox browser opens, loads the website but images are load too. I’m using Firefox 35, SeleniumBasic 2.09 and MS Excel 2010. I would be thank if you have information as for Firefox as Opera.
Congratulations for your website.!
Thank You again.
Greetings.!
There’s also another similar project one can try: https://github.com/daluu/seleniumexceladdin
FYI, there is also a VBScript alternative to Selenium VBA: http://htejera.users.sourceforge.net/vbswebdriver/
Hey Folk,
Q1) How to over write input value instead of appending to old value (Selenium WebDriver VBA)
Example: I would like to fill the input box in the form by using this syntax (VBA)
.FindElementByXPath(“//*[@id=’cf_1364′]”).SendKeys “Not Available”
but, “Not Available” String appending to old input value.
Q2) How to pass Shortcut keys (Selenium WebDriver VBA)
Example: like Application.keys “{TAb}”
hi, i’m zairy. thanks 4 sharing.
may u help me?
i hav try ur script to my project but i end up with this notification.
Public Sub seleniumtutorial()
Dim bot As New WebDriver
bot.Start “chrome”, “http://google.com”
bot.Get “/”
bot.TakeScreenshot.SaveAs (ActiveWorkbook.Path + “/screenshot.jpg”)
bot.Quit
‘quit method for closing browser instance.
End Sub
“SessionNotCreatedError
session note created exception
from unknown error: Runtime.executionContextCreated has invalid ‘context’ ….”
please help…
Your tutorial is so useful, but the project has not been updated until 2016.Did the author update the version somewhere else? Thank you
Hi,
I have several questions about the vba code. i’m not sure where to find the materials to learn to execute the code.
1) I want to click on this button with
I tried the findElementByName, then by ID. But it didn’t work. Should I try with findElementsbyClass. But how I do that?
2) I want to select Green in this code: Dyed
Pink
Green
White
Natural
Red
Blue
Orange
None
with: obj.FindElementByName(“SelectedFavoriteColor”).AsSelect.SelectByValue (G)
but it wouldn’t work. You know why? Please let me know. Thanks
Hi
How do i execute a javascript code to change the style of a HTML Element through my VBS?
Hi Ranjith,
Could you kindly explain me how can i solve the IE issu about the zoom in selenoum VBA?
Thanks in advance
how to attache a file by clicking browse button