python - Selenium 屏幕截图运行缓慢(Python)

标签 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

SOLVED:

The problem is not about selenium module, either screenshot. It is about phantomjs, after I start using chromedriver it is very fast and more efficent.

SOLUTION UPDATE:

The problem with phantomjs is disabling images. When I use --load-images=no I face with the memory leak issue and scripts gets really slower, without it there is no problem.

最佳答案

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

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')

关于python - Selenium 屏幕截图运行缓慢(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655967/

相关文章:

android - Kivy无限滚动

python - Matplotlib,损坏的图形表示错误

c# - C#中的字符串解析

java - 如何使用 Selenium Java 计算表中的行数

java - Selenium 网络驱动程序 : how to verify that there is a heading titled “Download” on the page wrapped in <h2> tags?

maven - 如何在我们的总结报告中添加更多的结果矩阵

python - 保存临时文件

python - 在 main 中链接函数调用。是 "Pythonic"吗?

c - 所有关于 C 内存管理

.net - 数字签名后托管 Windows 服务启动缓慢