带有 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/