อยากทราบวิธีการแก้ไข Error fullpage screenshot ใน selenium จะต้องแก้ไขยังไงครับ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: อยากทราบวิธีการแก้ไข Error fullpage screenshot ใน selenium จะต้องแก้ไขยังไงครับ

อยากทราบวิธีการแก้ไข Error fullpage screenshot ใน selenium จะต้องแก้ไขยังไงครับ

โดย บัวบุญ จันทะโคตร » 11/08/2017 11:01 am

ผมกำลังทำ fullpage screenshot ใน selenium เมื่อเขียนโค้ดเสร็จแล้วลองรันดูปรากฎว่าเกิด Error ขึ้น จะต้องแก้ไขยังไงครับ

ส่วนของโค้ด selenium.py

โค้ด: เลือกทั้งหมด

import sys
from selenium import webdriver
import re
import re,unittest,time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
import util

driver = webdriver.Chrome('/usr/local/lib/python2.7/site-packages/chromedriver')

driver.get("https://www.google.co.th/")
allweb = driver.current_url
driver.util.fullpage_screenshot("test.png")
ส่วนของโค้ด util.py

โค้ด: เลือกทั้งหมด

import os
import time

from PIL import Image

def fullpage_screenshot(driver, file):

        print("Starting chrome full page screenshot workaround ...")

        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")
        print("Total: ({0}, {1}), Viewport: ({2},{3})".format(total_width, total_height,viewport_width,viewport_height))
        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

                print("Appending rectangle ({0},{1},{2},{3})".format(ii, i, top_width, top_height))
                rectangles.append((ii, i, top_width,top_height))

                ii = ii + viewport_width

            i = i + viewport_height

        stitched_image = Image.new('RGB', (total_width, total_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]))
                print("Scrolled To ({0},{1})".format(rectangle[0], rectangle[1]))
                time.sleep(0.2)

            file_name = "part_{0}.png".format(part)
            print("Capturing {0} ...".format(file_name))

            driver.get_screenshot_as_file(file_name)
            screenshot = Image.open(file_name)

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

            print("Adding to stitched image with offset ({0}, {1})".format(offset[0],offset[1]))
            stitched_image.paste(screenshot, offset)

            del screenshot
            os.remove(file_name)
            part = part + 1
            previous = rectangle

        stitched_image.save(file)
        print("Finishing chrome full page screenshot workaround...")
        return True
ส่วนของ Error

โค้ด: เลือกทั้งหมด

Traceback (most recent call last):
  File "/home/com010/PycharmProjects/testselenium/seleniumtest.py", line 18, in <module>
    driver.util.fullpage_screenshot("test.png")
AttributeError: 'WebDriver' object has no attribute 'util'

ข้างบน