User Agents

Web browsers and everyday web tech, explained

Selenium Safari Screenshots: Capture Visible Elements

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Capturing only visible elements in Selenium for Safari requires targeting specific WebElements and utilizing the get_screenshot_as_file method (or its equivalent in your chosen language) rather than taking a full-page screenshot. Because Safari handles viewport rendering differently than Chromium-based browsers, the most reliable approach is to identify the specific DOM element, ensure it is scrolled into the visible area of the browser window, and then capture that specific element's coordinates.

What You'll Need

  • Safari browser with "Allow Remote Automation" enabled in the Develop menu.
  • Safaridriver installed and configured on your macOS system.
  • Selenium WebDriver library installed for your preferred language (Python, Java, C#, etc.).
  • A valid WebDriver instance initialized for Safari.
  • The CSS selector or XPath of the specific element you wish to capture.

Step-by-Step

  1. Enable Remote Automation: Open Safari, go to Settings > Advanced, and check "Show features for web developers". Then, go to the Develop menu in the top menu bar and select "Allow Remote Automation". Without this, Selenium cannot communicate with the browser.
  2. Initialize the Safari Driver: Set up your WebDriver instance. In Python, this is typically done via driver = webdriver.Safari(). Ensure your driver version matches your browser version to avoid compatibility errors.
  3. Navigate to the Target Page: Use driver.get("url") to load the page containing the element you want to capture.
  4. Locate the Element: Use a locator strategy to find the specific element. For example: element = driver.find_element(By.ID, "target-element-id").
  5. Scroll Element into View: Safari may not capture elements that are outside the current viewport. Use JavaScript execution to scroll the element into the center of the screen to ensure it is fully visible. Use the following script: driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", element).
  6. Implement a Wait: Add an explicit wait using WebDriverWait to ensure the element is not only present in the DOM but is fully rendered and visible. This prevents capturing a blank space or a loading spinner.
  7. Capture the Element Screenshot: Call the screenshot method directly on the element object rather than the driver object. In Python: element.screenshot("visible_element.png"). This tells Selenium to calculate the element's bounding box and crop the image accordingly.
  8. Verify and Close: Check the output file to ensure the crop is accurate and then close the session using driver.quit().

Common Mistakes

  • Capturing the Driver instead of the Element: Using driver.save_screenshot() captures the entire visible viewport, not the specific element. Always call the method on the element object.
  • Ignoring the Viewport: Attempting to screenshot an element that is "below the fold" without scrolling first. In Safari, this often results in an empty image or a screenshot of the wrong area.
  • Timing Issues: Taking the screenshot immediately after a page load or a click. If the element is animating into view, the screenshot may be blurred or cut off.
  • Incorrect Permissions: Forgetting to enable "Allow Remote Automation" in the Develop menu, which leads to a session not created error.

Tips

  • Use Center Alignment: When using scrollIntoView, using block: 'center' is safer than the default 'start' because it prevents the element from being hidden behind fixed headers or navigation bars.
  • Handle Overlays: If a sticky header or a pop-up covers your element, use execute_script to temporarily set the overlay's CSS display property to none before taking the shot.
  • High-DPI Screens: Be aware that on Retina displays, the resulting image may have a higher resolution than the CSS pixel dimensions of the element.
  • Dynamic IDs: If the element ID changes on every reload, use a stable CSS selector or a partial XPath match to ensure the locator doesn't break.

Troubleshooting

If you are receiving a blank image or an error, check the following:

  • ElementNotInteractableException: This usually means the element is hidden by another element or is not yet visible. Re-check your WebDriverWait logic.
  • Cropped Images: If the edges of your element are cut off, try adding a small padding to the element via JavaScript before the screenshot or ensure the browser window is maximized using driver.maximize_window().
  • Session Not Created: Ensure that no other instances of Safari are open that might interfere with the automation, and verify that the Safari version is up to date.
  • Coordinates Mismatch: If the screenshot captures the wrong part of the page, it is often due to a page layout shift. Try refreshing the page and adding a time.sleep(2) as a last resort to let the layout stabilize.

Frequently Asked Questions

Can I capture a full-page screenshot in Safari?

Standard Selenium save_screenshot only captures the visible viewport in Safari. For a full-page capture, you must either resize the window to the height of the body or use a third-party tool.

Why is my element screenshot coming out blurry?

This is often due to the device pixel ratio on macOS Retina displays. The image is captured at a higher resolution than the logical CSS pixels.

Does this method work for hidden elements?

No. If an element has display: none or visibility: hidden, Selenium cannot interact with it or take a screenshot. You must make it visible via JavaScript first.

How do I handle sticky headers blocking the element?

The best approach is to use scrollIntoView({block: 'center'}) or use JavaScript to temporarily hide the header element before executing the screenshot command.

Is Safaridriver separate from the browser?

Safaridriver is built into macOS and Safari; you do not need to download a separate binary like you do for ChromeDriver or GeckoDriver, but you must enable it in the settings.