from selenium import webdriver
import base64
import os
import time
def down_image(canvas_class, name):
mypath = os.path.dirname((os.path.abspath(__file__)))
js = 'return document.getElementsByClassName("{}")[0].toDataURL("image/png");'.format(
canvas_class)
image_data = browser.execute_script(js) # 执行js代码得到图片数据
image_base64 = image_data.split(",")[1] # 获得base64编码的图片信息
image_bytes = base64.b64decode(image_base64) # 将base64转为bytes类型
image_png="{}\\{}.png".format(mypath, name)
with open(image_png, "wb") as f:
f.write(image_bytes)
print("图片保存到",image_png)
if __name__ == '__main__':
browser = webdriver.Chrome(executable_path='chromedriver')
browser.get("http://localhost/test.html")
down_image("myCanvas", "temp")
down_image("myCanvas2"
python 下载 canvas里的图片
最新推荐文章于 2023-10-30 10:31:44 发布