Skip to content

Commit e5dd587

Browse files
authored
Merge pull request #3969 from seleniumbase/pdf-improvements
PDF improvements
2 parents aa32f85 + bb3c3fa commit e5dd587

File tree

6 files changed

+66
-23
lines changed

6 files changed

+66
-23
lines changed
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import base64
21
from seleniumbase import SB
3-
from selenium.webdriver.common.print_page_options import PrintOptions
42

5-
with SB(uc=True, test=True, ad_block=True) as sb:
3+
with SB(uc=True, test=True, pls="none") as sb:
64
url = "https://seleniumbase.io"
75
sb.activate_cdp_mode(url)
8-
sb.reconnect() # To access WebDriver methods
9-
print_options = PrintOptions()
10-
pdf_base64 = sb.driver.print_page(print_options)
11-
with open("downloaded_files/sb.pdf", "wb") as f:
12-
f.write(base64.b64decode(pdf_base64))
6+
sb.assert_title("SeleniumBase Docs")
7+
file_path = "downloaded_files/sb.pdf"
8+
sb.print_to_pdf(file_path)
9+
sb.assert_downloaded_file("sb.pdf")
10+
sb.assert_pdf_text(file_path, "SeleniumBase")

help_docs/method_summary.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ self.save_file_as(file_url, new_file_name, destination_folder=None)
341341
self.save_data_as(data, file_name, destination_folder=None)
342342
self.append_data_to_file(data, file_name, destination_folder=None)
343343
self.get_file_data(file_name, folder=None)
344+
self.print_to_pdf(name, folder=None)
344345
self.get_downloads_folder()
345346
self.get_browser_downloads_folder()
346347
self.get_downloaded_files(regex=None, browser=False)

requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ filelock~=3.16.1;python_version<"3.9"
1313
filelock>=3.19.1;python_version>="3.9"
1414
fasteners>=0.20
1515
mycdp>=1.2.0
16-
pynose>=1.5.4
16+
pynose>=1.5.5
1717
platformdirs>=4.3.6;python_version<"3.9"
1818
platformdirs>=4.4.0;python_version>="3.9"
1919
typing-extensions>=4.13.2
@@ -36,15 +36,15 @@ charset-normalizer>=3.4.3,<4
3636
urllib3>=1.26.20,<2;python_version<"3.10"
3737
urllib3>=1.26.20,<2.6.0;python_version>="3.10"
3838
requests==2.32.4;python_version<"3.9"
39-
requests>=2.32.5,<2.33;python_version>="3.9"
39+
requests~=2.32.5;python_version>="3.9"
4040
sniffio==1.3.1
4141
h11==0.16.0
4242
outcome==1.3.0.post0
4343
trio==0.27.0;python_version<"3.9"
44-
trio==0.30.0;python_version>="3.9"
45-
trio-websocket==0.12.2
44+
trio~=0.30.0;python_version>="3.9"
45+
trio-websocket~=0.12.2
4646
wsproto==1.2.0
47-
websocket-client==1.8.0
47+
websocket-client~=1.8.0
4848
selenium==4.27.1;python_version<"3.9"
4949
selenium==4.32.0;python_version>="3.9" and python_version<"3.10"
5050
selenium==4.35.0;python_version>="3.10"
@@ -82,7 +82,7 @@ rich>=14.1.0,<15
8282
coverage>=7.6.1;python_version<"3.9"
8383
coverage>=7.10.6;python_version>="3.9"
8484
pytest-cov>=5.0.0;python_version<"3.9"
85-
pytest-cov>=6.3.0;python_version>="3.9"
85+
pytest-cov>=7.0.0;python_version>="3.9"
8686
flake8==5.0.4;python_version<"3.9"
8787
flake8==7.3.0;python_version>="3.9"
8888
mccabe==0.7.0

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.41.5"
2+
__version__ = "4.41.6"

seleniumbase/fixtures/base_case.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7245,6 +7245,7 @@ def get_pdf_text(
72457245
page_search = [page]
72467246
else:
72477247
page_search = None
7248+
logging.getLogger("pdfminer").setLevel(logging.ERROR)
72487249
pdf_text = extract_text(
72497250
file_path,
72507251
password="",
@@ -7552,6 +7553,47 @@ def get_file_data(self, file_name, folder=None):
75527553
folder = constants.Files.DOWNLOADS_FOLDER
75537554
return page_utils._get_file_data(folder, file_name)
75547555

7556+
def print_to_pdf(self, name, folder=None):
7557+
"""Saves the current page as a PDF.
7558+
If no folder is specified, uses the folder where pytest was called.
7559+
If the folder provided doesn't exist, it will get created.
7560+
@Params
7561+
name - The name to give the PDF file. Must end in ".pdf".
7562+
folder - The directory where you want to save the PDF."""
7563+
import base64
7564+
from selenium.webdriver.common.print_page_options import PrintOptions
7565+
7566+
if not name.lower().endswith(".pdf"):
7567+
raise Exception('PDF name {%s} must end in ".pdf"!)' % name)
7568+
download_file_lock = fasteners.InterProcessLock(
7569+
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
7570+
)
7571+
if self.__is_cdp_swap_needed():
7572+
with download_file_lock:
7573+
with suppress(Exception):
7574+
shared_utils.make_writable(
7575+
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
7576+
)
7577+
if folder and not os.path.exists(folder):
7578+
os.makedirs(folder)
7579+
self.cdp.print_to_pdf(name, folder)
7580+
return
7581+
self.wait_for_ready_state_complete()
7582+
print_options = PrintOptions()
7583+
pdf_base64 = self.driver.print_page(print_options)
7584+
with download_file_lock:
7585+
with suppress(Exception):
7586+
shared_utils.make_writable(
7587+
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
7588+
)
7589+
if folder and not os.path.exists(folder):
7590+
os.makedirs(folder)
7591+
filename = name
7592+
if folder:
7593+
filename = os.path.join(folder, name)
7594+
with open(filename, "wb") as f:
7595+
f.write(base64.b64decode(pdf_base64))
7596+
75557597
def get_downloads_folder(self):
75567598
"""Returns the path of the SeleniumBase "downloaded_files/" folder.
75577599
Calling self.download_file(file_url) will put that file in here.

setup.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
'filelock>=3.19.1;python_version>="3.9"',
162162
'fasteners>=0.20',
163163
"mycdp>=1.2.0",
164-
"pynose>=1.5.4",
164+
"pynose>=1.5.5",
165165
'platformdirs>=4.3.6;python_version<"3.9"',
166166
'platformdirs>=4.4.0;python_version>="3.9"',
167167
'typing-extensions>=4.13.2',
@@ -184,15 +184,15 @@
184184
'urllib3>=1.26.20,<2;python_version<"3.10"',
185185
'urllib3>=1.26.20,<2.6.0;python_version>="3.10"',
186186
'requests==2.32.4;python_version<"3.9"',
187-
'requests>=2.32.5,<2.33;python_version>="3.9"',
187+
'requests~=2.32.5;python_version>="3.9"',
188188
'sniffio==1.3.1',
189189
'h11==0.16.0',
190190
'outcome==1.3.0.post0',
191191
'trio==0.27.0;python_version<"3.9"',
192-
'trio==0.30.0;python_version>="3.9"',
193-
'trio-websocket==0.12.2',
192+
'trio~=0.30.0;python_version>="3.9"',
193+
'trio-websocket~=0.12.2',
194194
'wsproto==1.2.0',
195-
'websocket-client==1.8.0',
195+
'websocket-client~=1.8.0',
196196
'selenium==4.27.1;python_version<"3.9"',
197197
'selenium==4.32.0;python_version>="3.9" and python_version<"3.10"',
198198
'selenium==4.35.0;python_version>="3.10"',
@@ -239,7 +239,7 @@
239239
'coverage>=7.6.1;python_version<"3.9"',
240240
'coverage>=7.10.6;python_version>="3.9"',
241241
'pytest-cov>=5.0.0;python_version<"3.9"',
242-
'pytest-cov>=6.3.0;python_version>="3.9"',
242+
'pytest-cov>=7.0.0;python_version>="3.9"',
243243
],
244244
# pip install -e .[flake8]
245245
# Usage: flake8
@@ -271,8 +271,10 @@
271271
'pdfminer.six==20250506;python_version>="3.9"',
272272
'cryptography==39.0.2;python_version<"3.9"',
273273
'cryptography==45.0.7;python_version>="3.9"',
274-
'cffi==1.17.1',
275-
"pycparser==2.22",
274+
'cffi==1.17.1;python_version<"3.9"',
275+
'cffi==2.0.0;python_version>="3.9"',
276+
'pycparser==2.22;python_version<"3.9"',
277+
'pycparser==2.23;python_version>="3.9"',
276278
],
277279
# pip install -e .[pillow]
278280
# (An optional library for image-processing.)

0 commit comments

Comments
 (0)