现代网页截图使用Python,Selenium& PhantomJS

时间:2016-08-12 11:49:55

标签: python selenium phantomjs

我正在尝试捕捉现代设计网页。以下是我的代码 -

def do_screen_capturing(url, screen_path, width, height):
    print("Capturing screen for.. "+url)
    driver = webdriver.PhantomJS()
    driver.set_script_timeout(60)
    if width and height:
        driver.set_window_size(width, height)
    driver.get(url)
    # print(driver)
    driver.execute_script('function whatever(){var a=document.body,b=document.documentElement,c=Math.max(a.scrollHeight,a.offsetHeight,b.clientHeight,b.scrollHeight,b.offsetHeight);window.scrollTo(0,c);var d=Math.max(a.scrollHeight,a.offsetHeight,b.clientHeight,b.scrollHeight,b.offsetHeight);c!=d&&whatever()}whatever();')
    time.sleep(1)
    el = driver.find_element_by_tag_name('body')
    for i in range(1,100,1):
       el.send_keys(Keys.ARROW_DOWN)
    time.sleep(1)
    driver.execute_script('function whatever(){var a=document.body,b=document.documentElement,c=Math.max(a.scrollHeight,a.offsetHeight,b.clientHeight,b.scrollHeight,b.offsetHeight);window.scrollTo(0,c);var d=Math.max(a.scrollHeight,a.offsetHeight,b.clientHeight,b.scrollHeight,b.offsetHeight);c!=d&&whatever()}whatever();')
    time.sleep(1)
    driver.execute_script('window.scrollTo(0, 0);')
    time.sleep(1)
    driver.save_screenshot(screen_path)
    print("Screenshot taken!")
    driver.service.process.send_signal(signal.SIGTERM)
    driver.quit()

当我使用Firefox(在我的本地机器上)作为驱动程序时,我得到一个漂亮的截图。看看使用Firefox作为驱动程序的screenshot。我删除了屏幕截图,因为它太大了。

问题是当我使用PhantomJS时。好像它没有执行我添加的那些JS脚本。看看使用PhantomJS作为驱动程序拍摄的screenshot

网站测试 - https://www.kitandace.com/ca/en/

有没有办法让PhantomJS的表现类似于Firefox?

0 个答案:

没有答案