世界那么大,我带你去看看

怎么自动化selenium截屏,截取长图哦,不是组合,不用phantomjs

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options

import time

def take_screenshot(url, save_fn=”capture.png”):
chrome_options = Options()
chrome_options.add_argument(‘–headless’)
chrome_options.add_argument(‘–disable-gpu’)
path = ‘/Users/Mac/Downloads/chromedriver.exe’
browser = webdriver.Chrome(path,chrome_options=chrome_options) # Get local session of firefox
#browser.set_window_size(1200, 3900)
browser.get(url) # Load page
browser.execute_script(“””
(function () {
var y = 0;
var step = 100;
window.scroll(0, 0);

function f() {
if (y < document.body.scrollHeight) {
y += step;
window.scroll(0, y);
setTimeout(f, 50);
} else {
window.scroll(0, 0);
document.title += “scroll-done”;
}
}

setTimeout(f, 1000);
})();
“””)

for i in xrange(30):
if “scroll-done” in browser.title:
break
time.sleep(1)

size = browser.find_element_by_xpath(“//body”).size
browser.set_window_size(size.get(“width”), size.get(“height”))
browser.save_screenshot(save_fn)
browser.close()

if __name__ == “__main__”:
take_screenshot(“http://www.studclub.cn/”)

未经允许不得转载:互联网骚操作 » 怎么自动化selenium截屏,截取长图哦,不是组合,不用phantomjs

分享到:更多 ()

评论 抢沙发

评论前必须登录!

世界那么大,跟着我去看看

联系我们阿里云运行