![](/img/trans.png)
[英]Take screenshot of full page with Selenium Python with chromedriver
[英]How to use python-selenium to take full page screenshot in mobile view
我已經通過 selenium webdriver 實現了整頁截圖。 代碼如下
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
jenkinsJobName = os.getenv("JOB_NAME")
url = "https://www.ccode.com/sg"
save_fn = "testResult.PNG"
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
option.add_argument("--window-size=1280,1024")
option.add_argument("--hide-scrollbars")
driver = webdriver.Chrome(chrome_options=option)
driver.get(url)
print(driver.title)
scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_screenshot(save_fn)
driver.quit()
那工作正常。
但我使用下面的代碼在移動設備中捕獲整頁截圖
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
jenkinsJobName = os.getenv("JOB_NAME")
url = "https://www.ccode.com/sg"
save_fn = "testResyyult.PNG"
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
mobile_emulation = {"deviceName": "iPhone 6"}
option.add_experimental_option("mobileEmulation", mobile_emulation)
option.add_argument("--auto-open-devtools-for-tabs")
driver = webdriver.Chrome(chrome_options=option)
driver.get(url)
print(driver.title)
scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_screenshot(save_fn)
driver.quit()
結果圖像只占頁面的一半,而不是像第一段代碼那樣的整頁。 在此處輸入圖像描述
如何修復此代碼?
謝謝
整頁截圖需要使用Firefox :
https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver
這是您修改的代碼:
我沒有在您的用例的上下文中運行此代碼,但我已將其用作我自己項目的一部分來獲取全屏屏幕截圖,您需要檢查您使用 firefox 描述的其他參數的兼容性。
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
jenkinsJobName = os.getenv("JOB_NAME")
url = "https://www.ccode.com/sg"
save_fn = "testResyyult.PNG"
option = webdriver.FirefoxOptions() # ------ Changes here
option.add_argument('--headless')
option.add_argument('--disable-gpu')
mobile_emulation = {"deviceName": "iPhone 6"}
option.add_experimental_option("mobileEmulation", mobile_emulation)
option.add_argument("--auto-open-devtools-for-tabs")
driver = webdriver.Firefox(options=option) # ------ Changes here
driver.get(url)
print(driver.title)
scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_full_page_screenshot(save_fn) # ------ Changes here This is the main change, search for this method in the documentation
driver.quit()
此外,您可能需要檢查您正在截取屏幕截圖的 URL。 好像沒有反應。
編輯:我還注意到你想要一個移動視圖。 不幸的是,我認為 firefox 即使在設置后也不會返回有效的移動視圖
option.enable_mobile
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.