- Aug 14, 2019
- Office Version
I have been working VBA to scrape website HTML code using the Microsoft HTML Object Library and the Microsoft Internet Controls Library. Now that support for Internet Explorer is being phased out, I am trying to switch my code over to scrape Google Chrome using the Selenium Type Library which is an open source download. The website I am trying to pull data from into excel has the following line of HTML code that I am interested in assigning to a variable:
I have been able to successfully assign a value to a variable using the following lines of code:
But what you will notice is that the the HTML code I provided has innerHTML which changes based on where your cursor is on the screen. The code above works but it only pulls the last instance where the innerHTML value is "Aug 3, 2021". What I want is something that will pull all of the data and not just the last instance. I tried to test it by changing the second to last line of code to Set Element = ch.FindElementByCss("div[class='enWFYd KDN9Hf'] [innerHTML='Feb 4, 2021']") but when I run the subroutine, I get an error saying that this element can't be found. I have also tried changing the last three lines as follows but it still is only pulling the last instance of this div tag.
<div jsname="iXWWee" class="enWFYd KDN9Hf" style="left: 39px; display: block; transform: translate3d(85px, 0px, 0px);">Feb 4, 2021</div>
Private ch As Selenium.ChromeDriver Sub Test() Set ch = New Selenium.ChromeDriver ch.Start , "https://www.google.com" ch.Get "https://www.google.com/search?q=us+vaccine+tracker&oq=us+&aqs=edge.0.69i59l2j69i57j69i60l4.2010j0j1&sourceid=chrome&ie=UTF-8" Dim Element As Selenium.WebElement Set Element = ch.FindElementByCss("div[class='enWFYd KDN9Hf']") Debug.Print Element.Attribute("innerHTML") End Sub
[LIST=1] [*]Dim Elements As Selenium.WebElements [*]Set Elements = ch.FindElementsByCss("div[class='enWFYd KDN9Hf']") [*]For Each Element in Elements [*]Debug.Print Element.Attribute("innerHTML") [*]Next Element [/LIST]