如何使用Python

时间:2017-12-04 12:47:42

标签: python selenium-webdriver screenshot

我需要截取整个网页的屏幕截图。这里的重要部分是我需要屏幕截图,以包含不适合屏幕的页面的全部内容。

数据包含多行(行)数据,由于数据长,因此它有一个滚动条。线条数量每次都不同,屏幕截图应相应地基于。

对于滚动的长网页,执行此任务非常简单。但是,当数据很大并且在滚动条下时,如何才能实现。

我想使用Python完成此操作。我使用以下代码使用Python捕获网页的屏幕截图。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1440x1440')
driver = webdriver.Chrome(executable_path=os.path.abspath('C:/Program Files (x86)/Python36/selenium/chromedriver/build/scripts-3.6/chromedriver.exe'),chrome_options=options)
driver.get("https://www.test.com") ##updated as a random test URL
time.sleep(60);
driver.save_screenshot('C:/Users/Dev/Desktop/Maxx/Snapshots/test.png')
driver.quit
print ("captured snapshot")

有关滚动条在浏览器上显示效果的数据。

enter image description here

3 个答案:

答案 0 :(得分:1)

from PIL import Image
from io import BytesIO

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def open_url(url):
    options = Options()

    options.headless = True

    driver = webdriver.Chrome(chrome_options=options)

    driver.maximize_window()
    driver.get(url)
    save_screenshot(driver, 'screen.png')

def save_screenshot(driver, file_name):
    height, width = scroll_down(driver)
    driver.set_window_size(width, height)
    img_binary = driver.get_screenshot_as_png()
    img = Image.open(BytesIO(img_binary))
    img.save(file_name)
    # print(file_name)
    print(" screenshot saved ")


def scroll_down(driver):
    total_width = driver.execute_script("return document.body.offsetWidth")
    total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
    viewport_width = driver.execute_script("return document.body.clientWidth")
    viewport_height = driver.execute_script("return window.innerHeight")

    rectangles = []

    i = 0
    while i < total_height:
        ii = 0
        top_height = i + viewport_height

        if top_height > total_height:
            top_height = total_height

        while ii < total_width:
            top_width = ii + viewport_width

            if top_width > total_width:
                top_width = total_width

            rectangles.append((ii, i, top_width, top_height))

            ii = ii + viewport_width

        i = i + viewport_height

    previous = None
    part = 0

    for rectangle in rectangles:
        if not previous is None:
            driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1]))
            time.sleep(0.5)
        # time.sleep(0.2)

        if rectangle[1] + viewport_height > total_height:
            offset = (rectangle[0], total_height - viewport_height)
        else:
            offset = (rectangle[0], rectangle[1])

        previous = rectangle

    return (total_height, total_width)

open_url("https://www.medium.com")

scroll_down 函数滚动到页面底部,并返回网页的总高度和宽度。

保存屏幕截图 功能设置窗口大小并使用枕头保存屏幕截图。

答案 1 :(得分:-2)

啊,啊,我明白了。我认为这将为您提供更多解决方案:https://stackoverflow.com/a/42531572/8737597

下面的代码示例:

    browser = webdriver.Firefox()
browser.get('http://stackoverflow.com/questions/37906704/taking-a-whole-page-screenshot-with-selenium-marionette-in-python')

# from here http://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript
js = 'return Math.max( document.body.scrollHeight, document.body.offsetHeight,  document.documentElement.clientHeight,  document.documentElement.scrollHeight,  document.documentElement.offsetHeight);'

scrollheight = browser.execute_script(js)

if verbose > 0: 
    print scrollheight

slices = []
offset = 0
while offset < scrollheight:
    if verbose > 0: 
        print offset

    browser.execute_script("window.scrollTo(0, %s);" % offset)
    img = Image.open(StringIO(browser.get_screenshot_as_png()))
    offset += img.size[1]
    slices.append(img)

    if verbose > 0:
        browser.get_screenshot_as_file('%s/screen_%s.png' % ('/tmp', offset))
        print scrollheight


screenshot = Image.new('RGB', (slices[0].size[0], scrollheight))
offset = 0
for img in slices:
    screenshot.paste(img, (0, offset))
    offset += img.size[1]

screenshot.save('/tmp/test.png')

答案 2 :(得分:-3)

你有没有尝试过:

driver.maximize_window()

另见:https://stackoverflow.com/a/26259329/8737597