{"id":15659,"date":"2024-03-21T11:14:27","date_gmt":"2024-03-21T05:44:27","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=15659"},"modified":"2025-06-16T04:33:32","modified_gmt":"2025-06-16T08:33:32","slug":"data-scraping-using-excel-vba-and-selenium","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/data-scraping-using-excel-vba-and-selenium\/","title":{"rendered":"Data Scraping using Excel VBA and Selenium"},"content":{"rendered":"\n<p>Selenium is widely recognized not only as a powerful tool for automation testing but also for data scraping, allowing users to extract information from HTML web pages. By simulating human interaction with web browsers, Selenium enables automated navigation through pages, input handling, and element selection to retrieve data directly from web elements. Many <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium certification<\/a> courses cover Selenium\u2019s capabilities in depth, equipping learners with the skills to use it effectively for both testing and data extraction tasks.<\/p>\n\n\n\n<p>This capability is particularly useful for data scraping tasks, where information such as text, images, URLs, or table data needs to be gathered systematically from multiple pages. Selenium\u2019s flexibility across various browsers and programming languages makes it an ideal choice for those needing precise control over web interactions to extract structured data effectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Steps to prepare Excel Macro<\/strong><\/h3>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;Open an Excel-based Macro.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20220628215226im_\/https:\/\/lh6.googleusercontent.com\/Ce2Os7UMSDOfGY1rrCuacLariMZqLhdCdJC-eRekMV4srWhEUalfV8pKGN55_7T15vh9QFUk9oKjDzbIuSqV6NYI7urreWEFd-kbvNLy4aGr2wyoRuowEgozCOUJ98srEY9cImJ4VK9i0bzmjQ\" alt=\"Data Scraping using Excel VBA and Selenium\" title=\"Data Scraping using Excel VBA and Selenium\"\/><\/figure>\n\n\n\n<p><strong>Step 2:&nbsp;<\/strong>Insert a new module.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20220628215226im_\/https:\/\/lh4.googleusercontent.com\/jicvwKQDdkCoG3MlkvFkN99heMDL-4F7QmMQdfH2zeBqJzcEBX7lQhjsIBMKroo-hm9C21b_iYD90LxfgIebgKxYm3ToyHUrdupkH-lyD8L0SLHgIGn1FZN3Urc6pWvHdsF6Gnmh4DOHAMunLw\" alt=\"Data Scraping using Excel VBA and Selenium\" title=\"Data Scraping using Excel VBA and Selenium\"\/><\/figure>\n\n\n\n<p><strong>Step 3:&nbsp;<\/strong>Initialize a new subroutine and name it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20220628215226im_\/https:\/\/lh6.googleusercontent.com\/4c8aBjl2Z0aOAM1PYkFvIjGIToZVvrSE_AC2u4K720xYEPqJNjlcIfL78qP48X7xS3t0dMvI3_8lL5mvo0x5kMBuYNaSJZSYGxXhu1iw0eEF0mHTcKfq_HX6621TysTqcQ2AaBWIZ59BsEeyPA\" alt=\"Data Scraping using Excel VBA and Selenium\" title=\"Data Scraping using Excel VBA and Selenium\"\/><\/figure>\n\n\n\n<p><strong>Step 4:&nbsp;<\/strong>Under Tools &gt; References. Add Selenium Type Library. These libraries help in opening google Chrome and&nbsp;developing a macro script.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20220628215226im_\/https:\/\/lh6.googleusercontent.com\/d7eade7-yhdkMXAftllz6KboZZflKiMGdYNks5ZYDFyc1fbtKK446y7hTppvYP9m7BWL-Y3dM_Nb8ONVBjs5FBXcawjq9M_wKIsJ9JZN06bIBCk_KixuEJQLzcmoNUJYso126eUfHpx9GGmYFA\" alt=\"Data Scraping using Excel VBA and Selenium\" title=\"Data Scraping using Excel VBA and Selenium\"\/><\/figure>\n\n\n\n<p>Now the excel file is ready to interact with the browser.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to open Google Chrome using VBA?<\/strong><\/h3>\n\n\n\n<p><strong>Step 1:&nbsp;<\/strong>Declare and initialize the variable in the subroutine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub test()\nDim driver As New WebDriver\nDim rc, cc, colC As Integer<\/code><\/pre>\n\n\n\n<p><strong>Step 2:&nbsp;<\/strong>For opening Google Chrome using VBA and Selenium, you need to write command a driver.start \u201dchrome\u201d and press F5.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub test()\nDim driver As New WebDriver\nDim rc, cc, colC As Integer\ndriver.Start \u201cChrome\u201d\nApplication.Wait Now + TimeValue(\u201c00:00:20\u201d)\nEnd Sub<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to open a Website in Chrome Browser using VBA?<\/strong><\/h3>\n\n\n\n<p>Now we need to pass the URL to access the website on the browser.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub test()\nDim driver As New WebDriver\nDim rc, cc, colC As Integer\ndriver.Start \u201cChrome\u201d\ndriver.get \u201chttps:\/\/www.google.com\u201d\nApplication.Wait Now + TimeValue(\u201c00:00:20\u201d)\nEnd Sub<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to scrape information from the website using VBA?<\/strong><\/h3>\n\n\n\n<p>If someone needs daily data scraping from a website, they can automate it so that a single click pulls updated data directly into Excel. Using Selenium with Excel VBA, a script can be set up to connect, navigate, and extract the latest data instantly. This saves time and ensures accurate, real-time information, ideal for tasks like tracking prices or monitoring sales.<\/p>\n\n\n\n<p>The first step is to Inspect Element and check how the data is structured in the source content of the website.<\/p>\n\n\n\n<p>Let us assume that the source code looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;table class=\"datatable\"&gt;\n&lt;thead&gt;\n&lt;tr&gt; \n&lt;th&gt;Company&lt;\/th&gt; \n&lt;th&gt;Group&lt;\/th&gt;\n&lt;th&gt;Pre Close (Rs)&lt;\/th&gt;\n&lt;th&gt;Current Price (Rs)&lt;\/th&gt; \n&lt;th&gt;% Change&lt;\/th&gt; &lt;\/tr&gt;\n<\/code><\/pre>\n\n\n\n<p>As it is clear that data is structured in the single HTML table. So we would design a macro which will pull the header information and corresponding data from the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/html-hypertext-markup-language\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/blog\/html-hypertext-markup-language\/\">HTML<\/a> table.<\/p>\n\n\n\n<p><br><strong>Step 1:&nbsp;<\/strong>To create a <code>for<\/code> loop that iterates through the HTML header information using Selenium, we will utilize <code>FindElementByClass()<\/code> and <code>FindElementByTag()<\/code> methods to locate and gather the header details of an HTML table. Here\u2019s how you could structure the code in VBA:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub test2()\nDim driver As New WebDriver\nDim rowc, cc, columnC As Integer\nrowc = 2\nApplication.ScreenUpdating = False\ndriver.Start \"chrome\"\ndriver.Get \"http:\/\/demo.h2kinfosys.com\/test\/web-table-element.php\"\nFor Each th In driver.FindElementByClass(\"dataTable\").FindElementByTag(\"thead\").FindElementsByTag(\"tr\")\ncc = 1\nFor Each t In th.FindElementsByTag(\"th\")\nSheet2.Cells(1, cc).Value = t.Text\ncc = cc + 1\nNext t\nNext th<\/code><\/pre>\n\n\n\n<p><strong>Step 2:&nbsp;<\/strong>To locate and extract table data using the Selenium driver in a similar manner, here\u2019s the revised VBA code. This script will locate the table headers and data, then write them to Sheet2 in Excel, starting from the specified cells.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub test2()\nDim driver As New WebDriver\nDim rowc, cc, columnC As Integer\nrowc = 2\nApplication.ScreenUpdating = False\ndriver.Start \"chrome\"\ndriver.Get\"http:\/\/demo.h2kinfosys\/test\/web-table-element.php\"\nFor Each th In driver.FindElementByClass(\"dataTable\").FindElementByTag(\"thead\").FindElementsByTag(\"tr\")\ncc = 1\nFor Each t In th.FindElementsByTag(\"th\")\nSheet2.Cells(1, cc).Value = t.Text\ncc = cc + 1\nNext t\nNext th\nFor Each tr In driver.FindElementByClass(\"dataTable\").FindElementByTag(\"tbody\").FindElementsByTag(\"tr\")\ncolumnC = 1\nFor Each td In tr.FindElementsByTag(\"td\")\nSheet2.Cells(rowc, columnC).Value = td.Text\ncolumnC = columnC + 1\nNext td\nrowc = rowc + 1\nNext tr\nApplication.Wait Now + TimeValue(\"00:00:20\")\nEnd Sub<\/code><\/pre>\n\n\n\n<p><div class=\"flex-shrink-0 flex flex-col relative items-end\"><div class=\"pt-0\"><div class=\"gizmo-bot-avatar flex h-8 w-8 items-center justify-center overflow-hidden rounded-full\"><div class=\"relative p-1 rounded-sm flex items-center justify-center bg-token-main-surface-primary text-token-text-primary h-8 w-8\"><svg width=\"41\" height=\"41\" viewBox=\"0 0 41 41\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"icon-md\" role=\"img\"><text x=\"-9999\" y=\"-9999\"><\/text><path d=\"M37.5324 16.8707C37.9808 15.5241 38.1363 14.0974 37.9886 12.6859C37.8409 11.2744 37.3934 9.91076 36.676 8.68622C35.6126 6.83404 33.9882 5.3676 32.0373 4.4985C30.0864 3.62941 27.9098 3.40259 25.8215 3.85078C24.8796 2.7893 23.7219 1.94125 22.4257 1.36341C21.1295 0.785575 19.7249 0.491269 18.3058 0.500197C16.1708 0.495044 14.0893 1.16803 12.3614 2.42214C10.6335 3.67624 9.34853 5.44666 8.6917 7.47815C7.30085 7.76286 5.98686 8.3414 4.8377 9.17505C3.68854 10.0087 2.73073 11.0782 2.02839 12.312C0.956464 14.1591 0.498905 16.2988 0.721698 18.4228C0.944492 20.5467 1.83612 22.5449 3.268 24.1293C2.81966 25.4759 2.66413 26.9026 2.81182 28.3141C2.95951 29.7256 3.40701 31.0892 4.12437 32.3138C5.18791 34.1659 6.8123 35.6322 8.76321 36.5013C10.7141 37.3704 12.8907 37.5973 14.9789 37.1492C15.9208 38.2107 17.0786 39.0587 18.3747 39.6366C19.6709 40.2144 21.0755 40.5087 22.4946 40.4998C24.6307 40.5054 26.7133 39.8321 28.4418 38.5772C30.1704 37.3223 31.4556 35.5506 32.1119 33.5179C33.5027 33.2332 34.8167 32.6547 35.9659 31.821C37.115 30.9874 38.0728 29.9178 38.7752 28.684C39.8458 26.8371 40.3023 24.6979 40.0789 22.5748C39.8556 20.4517 38.9639 18.4544 37.5324 16.8707ZM22.4978 37.8849C20.7443 37.8874 19.0459 37.2733 17.6994 36.1501C17.7601 36.117 17.8666 36.0586 17.936 36.0161L25.9004 31.4156C26.1003 31.3019 26.2663 31.137 26.3813 30.9378C26.4964 30.7386 26.5563 30.5124 26.5549 30.2825V19.0542L29.9213 20.998C29.9389 21.0068 29.9541 21.0198 29.9656 21.0359C29.977 21.052 29.9842 21.0707 29.9867 21.0902V30.3889C29.9842 32.375 29.1946 34.2791 27.7909 35.6841C26.3872 37.0892 24.4838 37.8806 22.4978 37.8849ZM6.39227 31.0064C5.51397 29.4888 5.19742 27.7107 5.49804 25.9832C5.55718 26.0187 5.66048 26.0818 5.73461 26.1244L13.699 30.7248C13.8975 30.8408 14.1233 30.902 14.3532 30.902C14.583 30.902 14.8088 30.8408 15.0073 30.7248L24.731 25.1103V28.9979C24.7321 29.0177 24.7283 29.0376 24.7199 29.0556C24.7115 29.0736 24.6988 29.0893 24.6829 29.1012L16.6317 33.7497C14.9096 34.7416 12.8643 35.0097 10.9447 34.4954C9.02506 33.9811 7.38785 32.7263 6.39227 31.0064ZM4.29707 13.6194C5.17156 12.0998 6.55279 10.9364 8.19885 10.3327C8.19885 10.4013 8.19491 10.5228 8.19491 10.6071V19.808C8.19351 20.0378 8.25334 20.2638 8.36823 20.4629C8.48312 20.6619 8.64893 20.8267 8.84863 20.9404L18.5723 26.5542L15.206 28.4979C15.1894 28.5089 15.1703 28.5155 15.1505 28.5173C15.1307 28.5191 15.1107 28.516 15.0924 28.5082L7.04046 23.8557C5.32135 22.8601 4.06716 21.2235 3.55289 19.3046C3.03862 17.3858 3.30624 15.3413 4.29707 13.6194ZM31.955 20.0556L22.2312 14.4411L25.5976 12.4981C25.6142 12.4872 25.6333 12.4805 25.6531 12.4787C25.6729 12.4769 25.6928 12.4801 25.7111 12.4879L33.7631 17.1364C34.9967 17.849 36.0017 18.8982 36.6606 20.1613C37.3194 21.4244 37.6047 22.849 37.4832 24.2684C37.3617 25.6878 36.8382 27.0432 35.9743 28.1759C35.1103 29.3086 33.9415 30.1717 32.6047 30.6641C32.6047 30.5947 32.6047 30.4733 32.6047 30.3889V21.188C32.6066 20.9586 32.5474 20.7328 32.4332 20.5338C32.319 20.3348 32.154 20.1698 31.955 20.0556ZM35.3055 15.0128C35.2464 14.9765 35.1431 14.9142 35.069 14.8717L27.1045 10.2712C26.906 10.1554 26.6803 10.0943 26.4504 10.0943C26.2206 10.0943 25.9948 10.1554 25.7963 10.2712L16.0726 15.8858V11.9982C16.0715 11.9783 16.0753 11.9585 16.0837 11.9405C16.0921 11.9225 16.1048 11.9068 16.1207 11.8949L24.1719 7.25025C25.4053 6.53903 26.8158 6.19376 28.2383 6.25482C29.6608 6.31589 31.0364 6.78077 32.2044 7.59508C33.3723 8.40939 34.2842 9.53945 34.8334 10.8531C35.3826 12.1667 35.5464 13.6095 35.3055 15.0128ZM14.2424 21.9419L10.8752 19.9981C10.8576 19.9893 10.8423 19.9763 10.8309 19.9602C10.8195 19.9441 10.8122 19.9254 10.8098 19.9058V10.6071C10.8107 9.18295 11.2173 7.78848 11.9819 6.58696C12.7466 5.38544 13.8377 4.42659 15.1275 3.82264C16.4173 3.21869 17.8524 2.99464 19.2649 3.1767C20.6775 3.35876 22.0089 3.93941 23.1034 4.85067C23.0427 4.88379 22.937 4.94215 22.8668 4.98473L14.9024 9.58517C14.7025 9.69878 14.5366 9.86356 14.4215 10.0626C14.3065 10.2616 14.2466 10.4877 14.2479 10.7175L14.2424 21.9419ZM16.071 17.9991L20.4018 15.4978L24.7325 17.9975V22.9985L20.4018 25.4983L16.071 22.9985V17.9991Z\" fill=\"currentColor\"><\/path><\/svg><\/div><\/div><\/div><\/div><\/p>\n\n\n\n<p><strong>Step 3:&nbsp;<\/strong>Once the macro is ready, pass, and assign the subroutine to excel button and exit the module. Label the button as any name.<\/p>\n\n\n\n<p><strong>Step 4:&nbsp;<\/strong>Press the labeled button to get the desired output.<\/p>\n\n\n\n<p><strong>Step 5:<\/strong>&nbsp;Compare the scraped results of Excel file with the results of the Chrome Site.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/web.archive.org\/web\/20220628215226\/https:\/\/www.h2kinfosys.com\/courses\/selenium-webdriver-junit-training-course\"><img fetchpriority=\"high\" decoding=\"async\" width=\"728\" height=\"90\" src=\"https:\/\/web.archive.org\/web\/20220628215226im_\/https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Certified-Selenium-Expert-2.jpeg\" alt=\"selenium training\" class=\"wp-image-6667\" title=\"Data Scraping using Excel VBA and Selenium\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Certified-Selenium-Expert-2.jpeg 728w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Certified-Selenium-Expert-2-300x37.jpeg 300w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, data scraping using Excel VBA and Selenium offers a powerful solution for automating the extraction of information from websites directly into spreadsheets. This combination brings together the flexibility of Selenium\u2019s web automation with the familiarity and functionality of Excel, enabling users to manage, analyze, and store data efficiently. <\/p>\n\n\n\n<p>Whether you&#8217;re gathering product details, tracking financial data, or conducting research, integrating VBA and Selenium streamlines the entire process, saving time and reducing manual work. By mastering these tools, you can create robust, reusable scripts that transform how you handle data. Embrace this approach to enhance productivity and gain valuable insights effortlessly!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Call to Action<\/strong><\/h2>\n\n\n\n<p>Need a faster way to extract data from websites into Excel? \u201c<a href=\"https:\/\/en.wikipedia.org\/wiki\/Data_scraping\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Data_scraping\" rel=\"nofollow noopener\" target=\"_blank\">Data Scraping<\/a> using Excel VBA and Selenium\u201d is the solution! By combining Selenium\u2019s web automation with Excel\u2019s VBA scripting, you can automate data extraction and populate spreadsheets with critical information in seconds. This approach is perfect for tasks like market research, financial tracking, and e-commerce analysis, making data collection more efficient and accurate.<\/p>\n\n\n\n<p>At H2K Infosys, our expert-led <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium training<\/a> will teach you how to integrate Selenium with Excel VBA, build reusable scripts, and handle real-world data extraction challenges. With hands-on guidance, you&#8217;ll gain the skills to automate complex scraping tasks, boost productivity, and transform how you manage data. Enroll today and elevate your automation expertise with H2K Infosys!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selenium is widely recognized not only as a powerful tool for automation testing but also for data scraping, allowing users to extract information from HTML web pages. By simulating human interaction with web browsers, Selenium enables automated navigation through pages, input handling, and element selection to retrieve data directly from web elements. Many Selenium certification [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15660,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-15659","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/15659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=15659"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/15659\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/15660"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=15659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=15659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=15659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}