【问题标题】:Take screenshot using helium in python在 python 中使用氦气截屏
【发布时间】:2021-05-22 20:20:48
【问题描述】:

我正在尝试在 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') 上成功,但这一行不处理特定元素。如何处理特定元素并对其进行快照?

【问题讨论】:

    标签: python selenium helium


    【解决方案1】:

    Helium 也暴露了所有的 selenium 方法,所以如果你检查 webelement 类

    https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html

    你可以看到有一个方法被调用

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

    【讨论】:

    • (3.0.5) 我用的是这个版本
    • 卸载 selenium 和 helium 然后重新安装 helium 看看
    • pip show helium 你可以使用这些命令查看安装的版本
    • @YasserKhalil 这很有趣,如果您将 chromedriver 保存在当前目录中并且不传递 chromeoptions,那么代码可以正常工作,否则您将不会收到任何命令错误!
    • @YasserKhalil 是的,为您的 chrome 版本下载最新的 chromedriver,与您的 python 文件保存在同一目录路径中。或者将 chromedriver 路径添加到环境变量 PATH 中。
    【解决方案2】:

    我通过裁剪特定元素找到了解决方法,但我欢迎任何其他想法(也许有更简单的解决方案)

    from selenium.webdriver.chrome.options import Options
    from helium import *
    from PIL import Image
    
    myCaseNumber = '181564540'
    url = 'https://eservices.moj.gov.kw/searchPages/searchCases.jsp'
    options = Options()
    options.binary_location = "C:/Program Files/Google/Chrome/Application/chrome.exe"
    
    browser = start_chrome(url, headless=False, options=options)
    element = browser.find_element_by_xpath("//*[@id='frmCaseNo']/div[2]/img")
    browser.execute_script("arguments[0].scrollIntoView();", element)
    location = element.location
    size = element.size
    get_driver().save_screenshot('Temp.png')
    x = location['x']
    y = location['y']
    width = location['x']+size['width']
    height = location['y']+size['height']
    im = Image.open('Temp.png')
    im = im.crop((int(x), int(y), int(width), int(height)))
    im.save('Number.png')
    

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 2015-08-14
      • 2021-03-09
      • 2020-08-29
      • 2019-02-08
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多