我目前正在尝试编写一个脚本(用于插入Django项目),以使用Selenium拍摄网站的完整页面截图
一切似乎都运行正常-路径(fullimsavepath)构建正常(因此我可以看到输出的print语句),selenium没有报告任何错误,脚本也可以正常退出。但是,
当我在提供的路径中查找实际屏幕截图时,它并不存在。我做错了什么?这与相对路径和绝对路径有关吗
在pipenv内部的Windows上运行Python 3.8
代码:
import time
import os
from sitehawk.settings import BASE_DIR
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from datetime import datetime, date
def takescreenshot(url='http://www.google.com', filename='testpng1'):
options = webdriver.ChromeOptions()
options.headless = True
# Need to replace executable path with environment variable or similar for production?
# At the moment it's an absolute path to the driver
driver = webdriver.Chrome(options=options,executable_path=r'C:/Users/gapbr/Documents/dev/sitehawk/sitehawk-project/screenshots/driver/chromedriver.exe')
driver.get(url)
# Set the path where the image should be saved
actualdate = datetime.now()
yr = actualdate.strftime('%Y')
mn = actualdate.strftime('%m')
filepng = filename+'.png'
fullimsavepath = os.path.join(BASE_DIR,'screenshots','captured-files',yr,mn,filepng)
print(fullimsavepath)
# Take the screenshot
S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment
driver.find_element_by_tag_name('body').screenshot(fullimsavepath)
driver.quit()
我对python知之甚少。但是,为了供您参考,我在我的C#项目中使用了下面的代码来截图。我希望你能从中得到一些想法:
也许Python的文字量比Java翻了一番。尝试在路径中将斜线加倍,如下所示:
相关问题 更多 >
编程相关推荐