I have a Streamlit app that mimics daily reports. Where each input widget is initialized with a value from the database.
I am trying to export/save my Streamlit app as a pdf and or screenshot.
At the moment, I am trying to create multiple screenshots of the page and then combine them into single full screenshot. To do so I need to move my window position by x amount.
I have tried the following script using Selenium,however scroll action doesnāt happen/work. I have also tried options with PAGE_DOWN Key.
Please suggest what could be the issue and potential solution.
Steps to reproduce
Code snippet:
driver = webdriver.Chrome()
# Navigate to the desired web page
url =' http://localhost:8501/'
driver.get(url)
scroll_x = 0 # Horizontal scroll position
scroll_y = 500 # Vertical scroll position
driver.execute_script(f"window.scrollTo({scroll_x}, {scroll_y});")
You should be able to use the menu in the top right corner and choose āprintā
If you want to do it automatically, and selenium isnāt working for you, you might try using Playwright, which supports full page screenshots. Screenshots | Playwright
Thank you for your fast response. I am able to use āprintā option in the top right corner. However, It produce inconsistent results, as it based on the screen resolution. Moreover, it does produce some blank pages at the start.
I have tested Playwright. Using following script, but it didnāt managed to produce full page (i.e. screenshot_full.png==screenshot.png), however it worked for some arbitrary website.
Hi Kawanovs,
Please visit the below youtube video. It uses headless option and lambda function.
Selenium Full Page ScreenShot Youtube Video : https://www.youtube.com/watch?v=iOpGGW__oz4
Thank you for your suggestion. It does work work some arbitrary website, but not for the streamlit app.
With the given code I get following error:
from selenium.webdriver.chrome.service import Service
chrome_options = Options()
chrome_options.add_argument("--headless") # Set any desired options
driver = webdriver.Chrome(options=chrome_options, service=Service(ChromeDriverManager().install()))
driver.implicitly_wait(10)
# Navigate to the desired web page
url = ' http://localhost:8501/' # Replace with your desired URL
driver.get(url)
# Small screenshot
driver.get_screenshot_as_file('report.png')
# Full screenshot
S = lambda x: driver.execute_script('return document.body.parentNode.scroll' + x)
driver.set_window_size(S('Width'), S('Height'))
driver.find_element(By.XPATH, '/html/body').screenshot('report_full.png')
selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {ācodeā:-32000,āmessageā:āCannot take screenshot with 0 height.ā}. I have also tried to use other elements, but I only have managed to get a small screenshot.
Any other suggestion are appreciated!
UPDATE:
Following solution was found. It does produce some bugs with sidebar animation and loading animation, but nothing critical.