MCPcopy Create free account
hub / github.com/seleniumbase/SeleniumBase / print_to_pdf

Method print_to_pdf

seleniumbase/fixtures/base_case.py:8134–8173  ·  view source on GitHub ↗

Saves the current page as a PDF. If no folder is specified, uses the folder where pytest was called. If the folder provided doesn't exist, it will get created. @Params name - The name to give the PDF file. Must end in ".pdf". folder - The directory where you w

(self, name, folder=None)

Source from the content-addressed store, hash-verified

8132 return page_utils._get_file_data(folder, file_name)
8133
8134 def print_to_pdf(self, name, folder=None):
8135 """Saves the current page as a PDF.
8136 If no folder is specified, uses the folder where pytest was called.
8137 If the folder provided doesn't exist, it will get created.
8138 @Params
8139 name - The name to give the PDF file. Must end in ".pdf".
8140 folder - The directory where you want to save the PDF."""
8141 import base64
8142 from selenium.webdriver.common.print_page_options import PrintOptions
8143
8144 if not name.lower().endswith(".pdf"):
8145 raise Exception('PDF name {%s} must end in ".pdf"!)' % name)
8146 download_file_lock = fasteners.InterProcessLock(
8147 constants.MultiBrowser.DOWNLOAD_FILE_LOCK
8148 )
8149 if self.__is_cdp_swap_needed():
8150 with download_file_lock:
8151 with suppress(Exception):
8152 shared_utils.make_writable(
8153 constants.MultiBrowser.DOWNLOAD_FILE_LOCK
8154 )
8155 if folder and not os.path.exists(folder):
8156 os.makedirs(folder)
8157 self.cdp.print_to_pdf(name, folder)
8158 return
8159 self.wait_for_ready_state_complete()
8160 print_options = PrintOptions()
8161 pdf_base64 = self.driver.print_page(print_options)
8162 with download_file_lock:
8163 with suppress(Exception):
8164 shared_utils.make_writable(
8165 constants.MultiBrowser.DOWNLOAD_FILE_LOCK
8166 )
8167 if folder and not os.path.exists(folder):
8168 os.makedirs(folder)
8169 filename = name
8170 if folder:
8171 filename = os.path.join(folder, name)
8172 with open(filename, "wb") as f:
8173 f.write(base64.b64decode(pdf_base64))
8174
8175 def get_downloads_folder(self):
8176 """Returns the path of the SeleniumBase "downloaded_files/" folder.

Callers 2

save_as_pdfMethod · 0.95
save_as_pdf_to_logsMethod · 0.95

Calls 3

__is_cdp_swap_neededMethod · 0.95
writeMethod · 0.80

Tested by

no test coverage detected