Skip to content

Commit

Permalink
Merge pull request #27 from feelgood-interface/master
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
sam4u3 authored Apr 26, 2023
2 parents a26a4ac + 9cee810 commit d4c02e1
Show file tree
Hide file tree
Showing 42 changed files with 2,807 additions and 82 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)

**Selenium Screenshot :**
**Selenium Screenshot:**

[![Open Source Love svg1](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badges/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
Expand All @@ -10,17 +10,17 @@
![Python package](https://github.com/sam4u3/Selenium_Screenshot/workflows/Python%20package/badge.svg)


The Selenium Screenshot is used to clipped Html Element using Selenium Webdriver
The Selenium Screenshot is used to clip Html pages and elements using Selenium.

**Installation :**
**Installation:**

`pip install Selenium-Screenshot`

This Package Support Python 3.6+ only
This package supports Python 3.6+ only.

**How to Use :**
**How to Use:**

**For Full Page ScreenShot :**
**For Full Page Screenshot:**

```python
from Screenshot import Screenshot
Expand All @@ -30,14 +30,14 @@ ob = Screenshot.Screenshot()
driver = webdriver.Chrome()
url = "https://github.com/sam4u3/Selenium_Screenshot/tree/master/test"
driver.get(url)
img_url = ob.full_Screenshot(driver, save_path=r'.', image_name='Myimage.png')
img_url = ob.full_screenshot(driver, save_path=r'.', image_name='Myimage.png')
print(img_url)
driver.close()

driver.quit()
```

**For Html Element Clipping :**
**For Html Element Clipping:**

````python
from Screenshot import Screenshot
Expand All @@ -58,7 +58,7 @@ driver.quit()

````

**For Html Element Clipping with Hiding Element :**
**For Html Element Clipping with Hiding Element:**

````python
from Screenshot import Screenshot
Expand All @@ -68,8 +68,8 @@ ob = Screenshot.Screenshot()
driver = webdriver.Chrome()
url = "https://github.com/sam4u3"
driver.get(url)
Hide_elements = ['class=avatar width-full height-full avatar-before-user-status'] # Use full class name
img_url = ob.full_Screenshot(driver, save_path=r'.', elements=Hide_elements, image_name='Myimage.png')
hide_elements = ['class=avatar width-full height-full avatar-before-user-status'] # Use full class name
img_url = ob.full_screenshot(driver, save_path=r'.', hide_elements=hide_elements, image_name='Myimage.png')
print(img_url)
driver.close()

Expand All @@ -82,12 +82,12 @@ driver.quit()
- Screenshot can take only 10000 of height of website


**Contact Information :**
**Contact Information:**

[Email:py.[email protected]](mailto::[email protected])

**Donation :**
**Donation:**

If you have found my softwares to be of any use to you, do consider helping me pay my internet bills. This would encourage me to create many such softwares.
If you have found my software to be of any use to you, do consider helping me pay my internet bills. This would encourage me to maintain and create more projects.

<a href="https://www.paypal.me/sam4u3" target="_blank"><img src="https://raw.githubusercontent.com/aha999/DonateButtons/master/Paypal.png" alt="Donate via PayPal" title="Donate via PayPal" /></a>
101 changes: 36 additions & 65 deletions Screenshot/Screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Screenshot:
"""
#================================================================================================================#
# Class: Screenshot #
# Purpose: Captured full and element screenshot using selenium #
# Purpose: Capture full and element screenshot using Selenium #
# a) Capture full webpage as image #
# b) Capture element screenshots #
#================================================================================================================#
Expand All @@ -30,30 +30,20 @@ def __init__(self):
"""
pass

@staticmethod
# Take temporary screenshot of the web page to get the size of the image
def __get_screen_size(driver: WebDriver) -> dict:
driver.get_screenshot_as_file('screenshot.png')
image = Image.open('screenshot.png')
width, height = image.size

return {'width': width, 'height': height}

def full_Screenshot(self, driver: WebDriver, save_path: str = '', image_name: str = 'selenium_full_screenshot.png',
elements: list = None, is_load_at_runtime: bool = False, load_wait_time: int = 5, multi_images: bool = False) -> str:
def full_screenshot(self, driver: WebDriver, save_path: str = '', image_name: str = 'selenium_full_screenshot.png',
hide_elements: list = None, is_load_at_runtime: bool = False, load_wait_time: int = 5) -> str:
"""
Take full screenshot of web page
Args:
driver: The Selenium web driver object
save_path: The path where to store screenshot
image_name: The name of screenshot image
elements: List of Xpath of elements to hide from web pages
is_load_at_runtime: Page Load at runtime
load_wait_time: The Wait time while loading full screen
multi_images: The flag is uses to capture multiple images and create single image in vertical format
driver: Web driver instance
save_path: Path where to save image
image_name: The name of the image
hide_elements: List of Xpath elements to hide from web page
is_load_at_runtime: Page loads at runtime
load_wait_time: The wait time while loading full screen
Returns:
str : The path of image
str: The image path
"""
image_name = os.path.abspath(save_path + '/' + image_name)

Expand All @@ -70,8 +60,9 @@ def full_Screenshot(self, driver: WebDriver, save_path: str = '', image_name: st
else:
break

self.hide_elements(driver, hide_elements)

if isinstance(driver, webdriver.Ie):
self.hide_elements(driver, elements)
required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
driver.set_window_size(required_width, final_page_height)
driver.save_screenshot(image_name)
Expand Down Expand Up @@ -100,53 +91,25 @@ def full_Screenshot(self, driver: WebDriver, save_path: str = '', image_name: st
rectangles.append((ii, i, top_width, top_height))
ii = ii + viewport_width
i = i + viewport_height
stitched_image = None
stitched_image = Image.new('RGB', (total_width, total_height))
previous = None
part = 0

# This value indicates if is the first part of the screenshot, or it is the rest of the screenshot
init_canvas = True
# Get the screenshot and save the dimensions of the image, this will be useful for create a
# correct canvas with correct dimensions
# and not show images with a wrong size or cut
size_screenshot = self.__get_screen_size(driver)
# Get temporary sum of height of the total pages
height_canvas = size_screenshot['height'] * len(rectangles)

# Compare if the screenshot it's only one part and assign the correct dimensions of the screenshot size
# Or assign the height of the total screenshot
if multi_images:
# Assign the height of the total screenshot at canvas even if not used at the moment
stitched_image = Image.new('RGB', (size_screenshot['width'], height_canvas))
else:
# Assign the dimensions corresponding to the size of the uniq screenshot
stitched_image = Image.new('RGB', (size_screenshot['width'], size_screenshot['height']))

# This constant is used top offset the image in the canvas
jump = round(size_screenshot['height'] / 2)
# With take multiple screenshots this value is used to adjust the position of the image in the canvas
# This value know update every time the screenshot is taken
top_offset = jump

for rectangle in rectangles:
if previous is not None:
driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1]))
time.sleep(1)
self.hide_elements(driver, elements)

self.hide_elements(driver, hide_elements)

file_name = "part_{0}.png".format(part)
driver.get_screenshot_as_file(file_name)
screenshot = Image.open(file_name)

if rectangle[1] + viewport_height > total_height:
offset = (rectangle[0], rectangle[1] + top_offset)
offset = (rectangle[0], total_height - viewport_height)
else:
if init_canvas:
offset = (rectangle[0], rectangle[1])
init_canvas = False
else:
offset = (rectangle[0], rectangle[1] + top_offset)
top_offset = jump + top_offset
offset = (rectangle[0], rectangle[1])

stitched_image.paste(screenshot, offset)
del screenshot
Expand All @@ -157,20 +120,24 @@ def full_Screenshot(self, driver: WebDriver, save_path: str = '', image_name: st
stitched_image.save(save_path)
return save_path

def get_element(self, driver: WebDriver, element: WebElement, save_location: str, image_name: str = 'cropped_screenshot.png', to_hide: list = None) -> str:
def get_element(self, driver: WebDriver, element: WebElement, save_path: str, image_name: str = 'cropped_screenshot.png', hide_elements: list = None) -> str:
"""
Usage:
Capture Element screenshot as a image
Capture element screenshot as an image
Args:
driver: Web driver instance
element : The element on web page to be captured
save_location : Path where to save image
element: The element on the web page to be captured
save_path: Path where to save image
image_name: The name of the image
hide_elements: List of Xpath elements to hide from web page
Returns:
img_url(str) : The path of image
img_url(str): The image path
Raises:
N/A
"""
image = self.full_Screenshot(driver, save_path=save_location, image_name='clipping_shot.png', elements=to_hide, multi_images=True)
image = self.full_screenshot(driver, save_path=save_path, image_name='clipping_shot.png', hide_elements=hide_elements)
# Need to scroll to top, to get absolute coordinates
driver.execute_script("window.scrollTo(0, 0)")
location = element.location
size = element.size
x = location['x']
Expand All @@ -182,8 +149,12 @@ def get_element(self, driver: WebDriver, element: WebElement, save_location: str

image_object = Image.open(image)
image_object = image_object.crop((int(x), int(y), int(width), int(height)))
img_url = os.path.abspath(os.path.join(save_location, f"{image_name}.png"))
img_url = os.path.abspath(os.path.join(save_path, image_name))
image_object.save(img_url)

image_object.close()
os.remove(image)

return img_url

@staticmethod
Expand All @@ -192,8 +163,8 @@ def hide_elements(driver: WebDriver, elements: list) -> None:
Usage:
Hide elements from web page
Args:
driver : The path of chromedriver
elements : The element on web page to be hide
driver: Web driver instance
elements: The elements on the web page to hide
Returns:
N/A
Raises:
Expand All @@ -205,11 +176,11 @@ def hide_elements(driver: WebDriver, elements: list) -> None:
sp_xpath = e.split('=')
if 'id=' in e.lower():
driver.execute_script(
"document.getElementById('{}').setAttribute('style', 'display:none;');".format(
"document.getElementById('{}').setAttribute('style', 'display:none !important;');".format(
sp_xpath[1]))
elif 'class=' in e.lower():
driver.execute_script(
"document.getElementsByClassName('{}')[0].setAttribute('style', 'display:none;');".format(
"document.getElementsByClassName('{}')[0].setAttribute('style', 'display:none !important;');".format(
sp_xpath[1]))
else:
print('For Hiding Element works with ID and Class Selector only')
Expand Down
Binary file added test/data/full_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/hide_element.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/paypal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,480 changes: 2,480 additions & 0 deletions test/data/website/test.html

Large diffs are not rendered by default.

Binary file added test/data/website/test_files/112872367.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/11541780.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/16545838.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/17972687.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/17972687_002.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/21293.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/29104974.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/31582257.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/33047641.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/48305608.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/53810625.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/61264851.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/66878007.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/website/test_files/68499531.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d4c02e1

Please sign in to comment.