Skip to content

Instantly share code, notes, and snippets.

@sh4nx0r
Created May 8, 2024 13:21
Show Gist options
  • Save sh4nx0r/955dea17cf332b448a23ea48ca667fbb to your computer and use it in GitHub Desktop.
Save sh4nx0r/955dea17cf332b448a23ea48ca667fbb to your computer and use it in GitHub Desktop.
Takes a website input and uses selenium driver to take the website snapshots (screenshot)
# You need to first check your chrome version. Open chrome browser and run chrome://version to find out the version and after that you can visit the https://googlechromelabs.github.io/chrome-for-testing/#stable to find the appropriate chromium drivers that is matching your chrome version. Download and extract the driver and give +x permission to the executable inside the extracted zip.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from PIL import Image
import io
def take_screenshot(url):
# Validate and format URL
if not url.startswith(('http://', 'https://')):
url = 'http://' + url
# Setup Chrome options
options = Options()
options.add_argument("--window-size=1920,1080") # Set the desired screenshot size
# Specify the path to chromedriver and set up the service
service = Service(executable_path='/home/kali/Downloads/chromedriver-linux64/chromedriver')
# Initialize the driver with the service and options
driver = webdriver.Chrome(service=service, options=options)
try:
# Open the URL
driver.get(url)
# Take a screenshot as binary data
screenshot = driver.get_screenshot_as_png()
# Close the browser
driver.quit()
# Open the image in memory
image = Image.open(io.BytesIO(screenshot))
# Create a thumbnail
image.thumbnail((256, 256))
# Display the thumbnail
image.show()
except Exception as e:
print(f"An error occurred: {e}")
driver.quit()
if __name__ == "__main__":
# Example usage
url = input("Enter the URL of the website: ")
take_screenshot(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment