赞
踩
全屏截图代码:
width = browser.execute_script("return document.documentElement.scrollWidth")
scroll_height = browser.execute_script('return document.body.parentNode.scrollHeight')
browser.execute_script('document.body.scrollTop=0')#回到顶部
browser.set_window_size(width, scroll_height)#全屏设置为网页完整大小便于全屏截图
browser.get_screenshot_as_file(path + filename)#截图
报错信息:
Traceback (most recent call last): File "url_topic.py", line 92, in seleniumurl get_url(browser, path, filename, article_url) File "url_topic.py", line 46, in get_url browser.get_screenshot_as_file(path + filename)#截图 File "/root/anaconda3/envs/rz_Env/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1321, in get_screenshot_as_file png = self.get_screenshot_as_png() File "/root/anaconda3/envs/rz_Env/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1357, in get_screenshot_as_png return b64decode(self.get_screenshot_as_base64().encode('ascii')) File "/root/anaconda3/envs/rz_Env/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1369, in get_screenshot_as_base64 return self.execute(Command.SCREENSHOT)['value'] File "/root/anaconda3/envs/rz_Env/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 425, in execute self.error_handler.check_response(response) File "/root/anaconda3/envs/rz_Env/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 10.000 (Session info: headless chrome=111.0.5563.64) Stacktrace: #0 0x55bc250df243 <unknown> #1 0x55bc24ea37a6 <unknown> #2 0x55bc24e8dd14 <unknown> #3 0x55bc24e8da30 <unknown> #4 0x55bc24e8c508 <unknown> #5 0x55bc24e8ccb2 <unknown> #6 0x55bc24eab32c <unknown> #7 0x55bc24f26512 <unknown> #8 0x55bc24f01082 <unknown> #9 0x55bc24f19932 <unknown> #10 0x55bc24f00e53 <unknown> #11 0x55bc24ed39ea <unknown> #12 0x55bc24ed4b2e <unknown> #13 0x55bc25133d5e <unknown> #14 0x55bc25137a80 <unknown> #15 0x55bc251198b0 <unknown> #16 0x55bc25138b63 <unknown> #17 0x55bc2510af75 <unknown> #18 0x55bc2515b998 <unknown> #19 0x55bc2515bb27 <unknown> #20 0x55bc25176c23 <unknown> #21 0x7ff3e58d5ea5 start_thread
改用以下代码截图,报错消失:
def save_fullscreenshot(browser,screen_shot_name): # We need the dimensions of the content page_rect = browser.execute_cdp_cmd('Page.getLayoutMetrics', {}) # parameters needed for ful page screenshot # note we are setting the width and height of the viewport to screenshot, same as the site's content size screenshot_config = {'captureBeyondViewport': True, 'fromSurface': True, 'clip': {'width': page_rect['contentSize']['width'], 'height': page_rect['contentSize']['height'], 'x': 0, 'y': 0, 'scale': 1}, } # Dictionary with 1 key: data base_64_png = browser.execute_cdp_cmd('Page.captureScreenshot', screenshot_config) # Write img to file with open(screen_shot_name, "wb") as f: f.write(base64.urlsafe_b64decode(base_64_png['data'])) save_fullscreenshot(browser, path + filename)#全屏截图
报错可能原因:设置网页全屏后,内容加载过多,会存在超时现象
疑似涉及browser.set_window_size(width, scroll_height)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。