Selenium截图工作慢(Python)

时间:2016-04-15 20:07:02

标签: python performance selenium phantomjs screenshot

带有selenium和phantomjs的脚本检查大约20个动态页面并在有更改时警告我没有屏幕截图部分快速工作但是当我想要获取页面的屏幕截图时需要大约1-2分钟来警告我并获得屏幕截图。有没有更好更快的方法来使用python截取页面的特定部分?

以下是我用于截屏的代码。

from selenium import webdriver
from PIL import Image

fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')

# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
fox.save_screenshot('screenshot.png') # saves screenshot of entire page
fox.quit()

im = Image.open('screenshot.png') # uses PIL library to open image in memory

left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']


im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
  

解决:

     

问题不在于selenium模块,无论是截图。它是   关于phantomjs,在我开始使用chromedriver之后它非常快且更多   efficent。

     

解决方案更新:

     

phantomjs的问题是禁用图像。我用的时候   --load-images=no我面临内存泄漏问题,脚本变得非常慢,没有它   没问题。

1 个答案:

答案 0 :(得分:3)

您可以通过在内存中裁剪屏幕截图而不先将其保存到文件来节省一些时间:

import StringIO
from selenium import webdriver
from PIL import Image

driver = webdriver.Firefox()
driver.get('http://stackoverflow.com')
element = driver.find_element_by_id('hlogo')

crop_points = driver.execute_script("""
    var r = arguments[0].getBoundingClientRect();
    return [r.left, r.top, r.left + r.width, r.top + r.height];
    """, element)

with Image.open(StringIO.StringIO(driver.get_screenshot_as_png())) as img :
    with img.crop(crop_points) as imgsub :
        imgsub.save(logo.png', 'PNG')