0

I'm trying to screenshot a specific ID which contains a table of weather forecasts for the week (https://forecast.weather.gov/MapClick.php?lat=41.8843&lon=-87.6324#.W5kg1ehKiUk, and the ID is "seven-day-forecast-body") but my code doesn't seem to work, and I've found few VBA-specific examples. Here's what I'm working with. Much appreciated, thank you.

Sub seleniumtutorial()

Dim bot As New WebDriver

bot.Start "IE", "https://forecast.weather.gov/MapClick.php?lat=41.8843&lon=-87.6324#.W5kg1ehKiUk"

bot.Get "/"

bot.FindElementById("seven-day-forecast-body").SaveAs (ActiveWorkbook.Path + "/chicago.jpg")

bot.Quit

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100

1 Answers1

0

Syntax is

bot.TakeScreenshot.SaveAs ActiveWorkbook.Path + "/chicago.jpg"

If you need to size to get only that part of the screen then you need to crop the screenshot image. Please see the answers here and VBA examples in search from here for pointers.

You can use Scroll functionality of selenium to centre your image on a particular element.


Scrolling element to centre:

Option Explicit
Sub seleniumtutorial()

    Dim bot As New WebDriver

    bot.Start "IE", "https://forecast.weather.gov/MapClick.php?lat=41.8843&lon=-87.6324#.W5kg1ehKiUk"

    bot.get "/"

    Dim element As Object
    Set element = bot.FindElementById("seven-day-forecast-body")

    Dim scrollElementIntoMiddle As String
    scrollElementIntoMiddle = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" _
                            + "var elementTop = arguments[0].getBoundingClientRect().top;" _
                            + "window.scrollBy(0, elementTop-(viewPortHeight/2));"

    bot.ExecuteScript scrollElementIntoMiddle, element

    bot.TakeScreenshot.SaveAs ActiveWorkbook.Path + "/chicago.jpg"

    bot.Quit

End Sub

Alternative is:

bot.ExecuteScript "arguments[0].scrollIntoView();", element
QHarr
  • 83,427
  • 12
  • 54
  • 101