python - 在 python 中使用氦气截屏
问题描述
我正在尝试使用 python 中的氦拍摄页面中特定元素的快照,这是我的代码
from selenium.webdriver.chrome.options import Options
from helium import *
url = 'exampleurl'
options = Options()
options.binary_location = "C:/Program Files/Google/Chrome/Application/chrome.exe"
browser = start_chrome(url, headless=False, options=options)
#.FindElementById("viewPane").ScrollIntoView True
element = browser.find_element_by_xpath("//*[@id='frmCaseNo']/div[2]/img")
#element.get_screenshot_as_file("Number.png")
#element.screenshot('Number.png')
#element.save_screenshot('Number.png')
#get_driver().save_screenshot('Number.png')
get_driver().element.save_screenshot('Number.png')
这条线成功使用氦气get_driver().save_screenshot('Number.png')
,但这条线不处理特定元素。如何处理特定元素并对其进行快照?
解决方案
Helium 也暴露了所有的 selenium 方法,所以如果你检查 webelement 类
你可以看到有一个方法叫做
webelement.screenshot("hellium.png")
,这会将元素屏幕截图保存为 helium.png
所以在你的情况下使用:
element = browser.find_element_by_xpath("//*[@id='frmCaseNo']/div[2]/img")
browser.execute_script("arguments[0].scrollIntoView();", element)
element.screenshot("Number.png")
element.screenshot("hellium.png")
完整代码:
from helium import *
from selenium.webdriver.chrome.options import Options
from shutil import copyfile
#copying it to current directory so that you don't have to do it
copyfile(r"C:\Users\Downloads\chromedriver.exe", "chromedriver.exe")
options=Options()
options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
browser = start_chrome("https://www.google.com",options=options)
browser.find_element_by_xpath("//body").screenshot("test.png")
推荐阅读
- javascript - ReactJS 通过状态中断 CSS 改变样式
- flutter - Flutter - 选择时更改图标颜色
- image - Flutter web 文件类型定义在多个类中
- postgresql - srid 不会改变(在 postgres 中)
- authentication - 如何使用 PKCE 进行授权和访问令牌请求
- javascript - 如何将 ReactDOM.findDOMNode 从类组件转换为函数组件
- python - QtWidget.grab() 在循环中调用时逐渐消耗内存
- android - AndroidX Security EncryptedSharedPreferences v1.1.0 /w API 21 问题
- javascript - Facebook SDK 与文本和变量共享
- ios - 我的 TableView 数据源函数没有被调用,我不知道为什么