selenium截图模糊_Selenium屏幕截图运行缓慢(Python)

使用selenium和phantomjs的脚本可以检查大约20个动态页面,并在没有屏幕截图部分的情况下警告我发生更改时可以快速运行,但是当我想要获取页面的屏幕截图时,大约需要1-2分钟来警告我并获取屏幕截图.有没有更好,更快的方法来使用python截取页面特定部分的屏幕截图?

这是我用于屏幕截图的代码.

from selenium import webdriver

from PIL import Image

fox = webdriver.Firefox()

fox.get('https://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']<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Selenium进行自动化测试时,有时会遇到警报框(Alert)弹出的情况。对于这种情况,我们可以使用Selenium的switch_to.alert()方法来切换到警报框并进行操作。然而,由于警报框通常会遮挡页面内容,因此在进行时可能无法正确地取页面内容。 为解决这个问题,可以尝试以下几种方法: 1. 利用JS代码关闭警报框 在Selenium中,我们可以使用execute_script()方法来执行JavaScript代码。因此,如果警报框不是必须要处理的,我们可以通过JS代码来关闭它,然后再进行。 示例代码: ```python alert = driver.switch_to.alert alert.dismiss() # 关闭警报框 # 进行 driver.save_screenshot('screenshot.png') ``` 2. 使用Pillow库对进行修剪 Pillow是Python中一个强大的像处理库,我们可以使用它对进行修剪,只保留我们需要的部分。具体操作如下: ```python from PIL import Image # 获取页面大小 width = driver.execute_script("return document.documentElement.scrollWidth") height = driver.execute_script("return document.documentElement.scrollHeight") # 进行 screenshot = driver.get_screenshot_as_png() image = Image.open(BytesIO(screenshot)) # 修剪 left = 0 top = 0 right = width bottom = height - 100 # 剔除警报框的高度 image = image.crop((left, top, right, bottom)) # 保存 image.save('screenshot.png') ``` 通过以上两种方法,我们可以在Selenium中正确地取页面内容,避免了警报框对的影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值