Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium屏幕截图运行缓慢(Python)_Python_Performance_Selenium_Phantomjs_Screenshot - Fatal编程技术网

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

Selenium屏幕截图运行缓慢(Python),python,performance,selenium,phantomjs,screenshot,Python,Performance,Selenium,Phantomjs,Screenshot,使用selenium和phantomjs编写的脚本,可以检查大约20个动态页面,并在出现更改时向我发出警告,但在没有屏幕截图部分的情况下,脚本运行速度很快,但当我想要获取页面的屏幕截图时,向我发出警告和获取屏幕截图大约需要1-2分钟。有没有更好更快的方法用python拍摄页面特定部分的截图 下面是我用于截图的代码 from selenium import webdriver from PIL import Image fox = webdriver.Firefox() fox.get('htt

使用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之后,它非常快,而且速度更快 效率

解决方案更新:

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


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

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

您是否已确定代码的哪一部分花费的时间太长?硒截图,还是PIL作物?你确定当时的记忆发生了什么吗?当时CPU资源发生了什么变化?是的,我做了。对于phantomjs,内存增加到3gb,并一直保持到3gb,直到我关闭它,我在活动监视器中看到5gb,但这只是一次,cpu消耗也增加了很多。情况肯定不正常。我认为问题在于Selenium屏幕截图部分,因为我跟随它,看到它在第二个文件夹中裁剪整个图像。谢谢你的回答,但最快的部分是裁剪。我真的不知道为什么,但phantomjs会消耗内存,这是不正常的,而且运行速度非常慢。我使用的osx内存为4gb,i5和phantomjs的内存消耗可达3gb。怎么可能呢?铬合金怎么样?情况也一样糟糕吗?可能是内存泄漏,但如果没有可复制的示例,很难说。你不会相信我,但即使我在加载图像时使用它,它也只有35mb。同样的剧本,同样的条件。我真的不明白幻影有什么问题。这是最新的。所以问题不在于脚本或selenium,问题完全在于phantomjs。我在osx上,所以我不能让chromedriver无头,这就是为什么我必须使用phantomjs。firefox的内存消耗大约为400mb。也不能使用headless。我只能建议您使用Chrome而不是PhantomJS。如果你真的不想要这个窗口,那么你应该在屏幕外启动浏览器来隐藏它。