-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch_17.py
More file actions
46 lines (36 loc) · 1.52 KB
/
Copy pathscratch_17.py
File metadata and controls
46 lines (36 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
# 设置WebDriver路径
webdriver_path = r'C:\Users\ADMIN\AppData\Local\Programs\Python\Python311\chromedriver.exe'
# 配置Chrome选项
chrome_options = Options()
chrome_options.add_argument("--headless") # 无头模式,不显示浏览器窗口
chrome_options.add_argument("--window-size=992,1424") # 设置窗口大小为992x1424
# 初始化WebDriver
service = Service(webdriver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
# 指定HTML文件所在的文件夹路径
html_folder_path = r'./assets\cards_html'
# 指定截图保存的文件夹路径
screenshot_folder_path = r'./assets\cards_pic'
# 确保截图文件夹存在
os.makedirs(screenshot_folder_path, exist_ok=True)
# 遍历文件夹中的所有HTML文件
for filename in os.listdir(html_folder_path):
if filename.endswith(".html"):
# 构建文件的完整路径
file_path = os.path.join(html_folder_path, filename)
# 访问HTML文件
driver.get(file_path)
# 等待页面加载完成,根据需要调整等待时间
time.sleep(0.1)
# 截图并保存
screenshot_path = os.path.join(screenshot_folder_path, os.path.splitext(filename)[0] + '.png')
driver.save_screenshot(screenshot_path)
print(f"Screenshot saved as: {screenshot_path}")
# 关闭WebDriver
driver.quit()