python - Selenium - 获取正文下一级元素的 XPath 列表
问题描述
我想直接在body
网页标签下截取每个元素的屏幕截图
我已经编写了示例脚本 -
from selenium import webdriver
from PIL import Image
from io import BytesIO
fox = webdriver.Firefox()
fox.get('http://google.com/')
# get list of elements
elements = fox.find_elements_by_xpath("//html/body")
在此之后,我如何找到每个元素的 XPath 并截图?
给定单个元素的 XPath,我有脚本来截取屏幕截图
element = fox.find_element_by_xpath("//*[@id=\"hplogo\"]")
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()
im = Image.open(BytesIO(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
解决方案
您说要抓取元素的屏幕截图有点令人困惑
“直接在
body
标签下”
但是在第二个代码片段中,您想要获取'//*[@id=\"hplogo\"]'
不是. 所以我的解决方案是基于一个假设,即您希望将“一些元素”保存为图像,但不一定只保存.body
body
假设您想要获取所有元素并稍后处理过滤。只需抓住以下(不那么值得截图的)script
元素之外的所有元素body
:
elements = fox.find_elements_by_xpath('//html/body//*[not(self::script)]')
然后你可以在没有 PIL 的情况下保存它们,幸运的是 selenium 能够保存关于给定元素的屏幕截图:
# leading zeroes for filenames
padding = len(str(len(elements)))
for i, element in enumerate(elements):
# you probably don't want a 0 byte screenshot or a try/except block
if not(element.rect['height'] and element.rect['width']):
continue
# don't forget to specify your target dir
with open(os.path.join(target_dir, f'{str(i).zfill(padding)}.png'), 'wb') as f:
f.write(element.screenshot_as_png)
您可以通过仅保存非重复项来改进这一点,并且拥有正确的元素列表也可以节省大量时间。
推荐阅读
- amazon-web-services - 从 AWS Cognito 解码 JWT 公开 poolId 和客户端应用程序密钥
- android - 将 Smart Lock 与 Firebase 身份验证电子邮件+密码帐户结合使用
- python - 如何正确处理石墨烯错误?
- python - Python请求,没有协议的get()?
- c++ - 如何通过opencv cv :: calibrateCamera通过索引返回访问值?
- c - 如何通过将用户的主题作为c语言中的另一个结构成员来分配结构成员中的部分
- c# - 我可以删除使用 Filestream 打开的文件的第一部分吗?
- single-sign-on - 为 CAS Apereo 版本 cas-overlay-template-6.0 启用 REST 服务
- css - 更改边框自动完成 vuetify
- javascript - 按下 Enter 键时使用 JavaScript 触发按钮单击