selenium-实现全网页截图1234567891011121314151617181920212223242526272829303132import timefrom PIL import Imagefrom selenium import webdriver'''窗口大小自行更改即可如: width = int(3000) height = int(1164) '''url='https://cn.bing.com/search?q=pyspark'# 访问浏览器获取数据options = webdriver.ChromeOptions()options.add_argument('--ignore-certificate-errors')options.add_argument('--lang=zh-CN')options.add_argument('--headless') #无头模式即使不打开浏览器 或采用 PhantomJSdriver = webdriver.Chrome(chrome_options=options)driver.get(url)time.sleep(4)# 自动获取浏览器窗口大小 也可自定义大小 如: width = int(1164) height = int(2000)width = driver.execute_script("return document.documentElement.scrollWidth")height = driver.execute_script("return document.documentElement.scrollHeight")driver.set_window_size(width,height) #修改浏览器窗口大小# 定义方法实现网页全截图def all_Webpage_Screenshot(): # 获取整个网页截图 driver.get_screenshot_as_file('./py测试目录/1.png') print("整个网页尺寸:height={},width={}".format(height,width)) im = Image.open('./py测试目录/1.png') print("截图尺寸:height={},width={}".format(im.size[1],im.size[0]))all_Webpage_Screenshot() 效果: