How to use python-selenium to take full page screenshot in mobile view

Viewed 401

I have implemented full page screenshot by selenium webdriver. The code as follow

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()

that is working fine.

But I use below code to capture full page screenshot in mobile


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()

The result image just take half of page, not for full page like in the first segement code. enter image description here

How can I fix this code?

Thanks

1 Answers

You need to use Firefox for full-page screenshots:

https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver

Here is your code modified:

I have not run this code in context to your use case but I have used it as a part of my own project to get fullscreen screenshots, you will need to check the compatibility of other parameters that you have described with 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()

Also you might want to check the URL that you are taking a screenshot of. Seems like it is unresponsive.

Edit: I also noticed that you want a mobile view. Unfortunately, I think firefox does not return a valid mobile view even after setting

option.enable_mobile
Related