简体   繁体   English

使用 Chrome 驱动程序使用 Selenium Python 截取完整的 web 页面

[英]Take screenshot of full web page with Selenium Python with Chrome driver

I want to take a screenshot of the full page of a website.我想截取一个网站的整个页面。 Below are the website conditions以下是网站条件

  1. It requires authentication with a sign in form它需要使用登录表单进行身份验证
  2. The website I want to screenshot is rendered by Javascript (dynamic rendering), not static rendering (inside the body tag is just script tags)我要截图的网站是Javascript渲染的(动态渲染),不是static渲染的( body标签里面就是script标签)

I did try some solutions I can find, but none of them work as I desired.我确实尝试了一些我能找到的解决方案,但它们都没有按我的意愿工作。 Below are some attempt I tried以下是我尝试过的一些尝试

  1. Link This one required using headless chrome, which make me unable to fill the sign in form 链接这个需要使用 headless chrome,这让我无法填写登录表格
  2. When I try to remove the headless options, I get this error "message":"Cannot take screenshot with 0 height."当我尝试删除无头选项时,我收到此错误"message":"Cannot take screenshot with 0 height." I guess this is because the content is rendered by Javascript.我猜这是因为内容是由 Javascript 渲染的。 Note that I already give it some time to render with time.sleep(5)请注意,我已经给了它一些时间来渲染time.sleep(5)
  3. When I try to choose another element, the result is varying.当我尝试选择另一个元素时,结果会有所不同。 Some gives same error message as above, some give me the screenshot but not full page, just the visible part (same result as using browser.save_screenshot() ).有些给出了与上面相同的错误消息,有些给出了屏幕截图但不是整页,只是可见部分(与使用browser.save_screenshot()的结果相同)。 Below is the code I tried.下面是我试过的代码。

 def S(X): return browser.execute_script( 'return document.querySelector("#main-layout-content").scroll'+X ) browser = webdriver.Chrome() browser.get('some link go here') browser.set_window_size(S('Width') + 100, S('Height') + 1000) #The print statement below work print(S('Height')) browser.find_element_by_id('main-layout-content').screenshot('web_screenshot.png')

Is there any way to achieve what I need?有什么办法可以达到我的需要吗?

Here is what I did to get the full-size screenshot of a webpage这是我为获取网页的全尺寸屏幕截图所做的

from selenium import webdriver
browser = webdriver.Chrome()

# We go to the webpage here and wait 2 seconds for fully load
browser.get('https://someURL')
time.sleep(2)

# Where to save the picture
directory = '/home'
# Video title
title = 'Hello world'

try:
        # We try to get the top-level component in which all out page is its children
        # This is different for each website
        elem = browser.find_element_by_id('main-layout-content')
        # Get the height of the element, and adding some height just to be sage
        total_height = elem.size['height'] + 1000
        # Set the window size - what is the size of our screenshot
        # The width is hardcoded because the screensize is fixed for each computer
        browser.set_window_size(1920, total_height)
        # Wait for 2 seconds
        time.sleep(2)
        # Take the screenshot
        browser.find_element_by_id(
            'main-layout-content').screenshot(f'./{directory}/{title}.png')
except SystemError as err:
        print('Take screenshot error at' + title)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM