我一直在探索硒以获取网站的屏幕截图,并通过比较屏幕截图的某些部分来检测任何更改,而不是比较完整的屏幕截图,因为我想忽略广告和动态部分。
我的策略是首先获取完整的屏幕截图,然后通过指定像素坐标来选择特定部分。
由于某些原因,即使假定图像是相同的,似乎也有微小的变化,这使我相信硒包在while循环运行之间以某种方式截取屏幕截图会有所不同。
请注意,我有time.sleep(30)
等待网站完全加载,然后再截屏。您是否认为如果网站花费的时间比给定的加载时间长,可能会导致截屏不同,从而导致错误警报?
下面是我的代码。
from PIL import Image
import screenshot_capture as sc
from crop_test import crop
import time
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from gmaillogin_test import sendemail
i =1
sc.screenshot_capture(i,'website address')
image_name = 'screen_shot_temp' + str(i) +'.png'
crop(image_name, (354, 170, 1517, 735), 'cropped_temp' + str(i) +'.png')
#crop(image_name, (240, 358, 1153, 1100), 'cropped' + str(i) +'.png')
snippedimage_name = 'cropped_temp' + str(i) +'.png'
snippedimage = mpimg.imread(snippedimage_name)
while True:
print(i)
i += 1
snippedpreimage = snippedimage
#time.sleep(600)
sc.screenshot_capture(i,'website address')
image_name = 'screen_shot_temp' + str(i) +'.png'
crop(image_name, (354, 170, 1517, 735), 'cropped_temp' + str(i) +'.png')
snippedimage_name = 'cropped_temp' + str(i) +'.png'
snippedimage = mpimg.imread(snippedimage_name)
if np.any(snippedpreimage != snippedimage):
print('something changed')
sendemail()
下面是“ screenshot_capture”函数。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
def screenshot_capture(itnum,address):
driver = webdriver.Chrome()
driver.get(address)
#pause 30 second to let page load
time.sleep(30)
#get scroll Height
height = driver.execute_script("return Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight )")
print(height)
#close browser
driver.close()
#Open another headless browser with height extracted above
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument(f"--window-size=1920,{height}")
chrome_options.add_argument("--hide-scrollbars")
driver = webdriver.Chrome(options=chrome_options)
driver.get(address)
#pause 30 second to let page loads
time.sleep(30)
#save screenshot
imagetitle = 'screen_shot_temp' + str(itnum) +'.png'
driver.save_screenshot(imagetitle)
driver.close()
下面是函数“ crop”。
from PIL import Image
def crop(image_path, coords, saved_location):
"""
@param image_path: The path to the image to edit
@param coords: A tuple of x/y coordinates (x1, y1, x2, y2)
@param saved_location: Path to save the cropped image
"""
image_obj = Image.open(image_path)
cropped_image = image_obj.crop(coords)
cropped_image.save(saved_location)