Skip to content

Commit 677b2b0

Browse files
authored
Merge pull request #3974 from seleniumbase/more-pdf-improvements
More PDF improvements
2 parents 7695bcb + 1ad7601 commit 677b2b0

File tree

13 files changed

+120
-20
lines changed

13 files changed

+120
-20
lines changed

README.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,43 +65,68 @@
6565

6666
--------
6767

68-
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_google.py">raw_google.py</a>, which performs a Google search:</p>
68+
<p align="left">📗 For performing a Google Search without hitting the "unusual traffic" page, you can use SeleniumBase UC Mode. Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_google.py">SeleniumBase/examples/raw_google.py</a>, which exports the search results into different formats (PDF, HTML, PNG):</p>
6969

7070
```python
7171
from seleniumbase import SB
7272

7373
with SB(test=True, uc=True) as sb:
7474
sb.open("https://google.com/ncr")
75-
sb.type('[title="Search"]', "SeleniumBase GitHub page\n")
76-
sb.click('[href*="github.com/seleniumbase/"]')
77-
sb.save_screenshot_to_logs() # ./latest_logs/
75+
sb.type('[title="Search"]', "SeleniumBase GitHub page")
76+
sb.click("div:not([jsname]) > * > input")
7877
print(sb.get_page_title())
78+
sb.sleep(2) # Wait for the "AI Overview" result
79+
if sb.is_text_visible("Generating"):
80+
sb.wait_for_text("AI Overview")
81+
sb.save_as_pdf_to_logs() # Saved to ./latest_logs/
82+
sb.save_page_source_to_logs()
83+
sb.save_screenshot_to_logs()
7984
```
8085

8186
> `python raw_google.py`
8287
83-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_google.py"><img src="https://seleniumbase.github.io/cdn/gif/google_search.gif" alt="SeleniumBase Test" title="SeleniumBase Test" width="480" /></a>
88+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_google.py"><img src="https://seleniumbase.github.io/cdn/img/google_sb_result.png" alt="SeleniumBase on Google" title="SeleniumBase on Google" width="440" /></a>
8489

8590
--------
8691

87-
<p align="left">📗 Here's an example of bypassing Cloudflare's challenge page: <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_gitlab.py">SeleniumBase/examples/cdp_mode/raw_gitlab.py</a></p>
92+
<p align="left">📗 Here's an example of bypassing Cloudflare's challenge page with UC Mode + CDP Mode: <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_gitlab.py">SeleniumBase/examples/cdp_mode/raw_gitlab.py</a></p>
8893

8994
```python
9095
from seleniumbase import SB
9196

9297
with SB(uc=True, test=True, locale="en") as sb:
9398
url = "https://gitlab.com/users/sign_in"
9499
sb.activate_cdp_mode(url)
95-
sb.sleep(1)
100+
sb.sleep(2.2)
96101
sb.uc_gui_click_captcha()
97-
sb.sleep(2)
102+
sb.assert_text("Username", '[for="user_login"]', timeout=3)
103+
sb.assert_element('label[for="user_login"]')
104+
sb.highlight('button:contains("Sign in")')
105+
sb.highlight('h1:contains("GitLab.com")')
106+
sb.post_message("SeleniumBase wasn't detected", duration=4)
98107
```
99108

100109
<img src="https://seleniumbase.github.io/other/cf_sec.jpg" title="SeleniumBase" width="332"> <img src="https://seleniumbase.github.io/other/gitlab_bypass.png" title="SeleniumBase" width="288">
101110

111+
<p align="left">📙 You can also use SeleniumBase's pure CDP Mode, which doesn't use chromedriver, Selenium, or a Python context manager at all: <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_cdp_gitlab.py">SeleniumBase/examples/cdp_mode/raw_cdp_gitlab.py</a></p>
112+
113+
```python
114+
from seleniumbase import sb_cdp
115+
116+
url = "https://gitlab.com/users/sign_in"
117+
sb = sb_cdp.Chrome(url)
118+
sb.sleep(2.5)
119+
sb.gui_click_captcha()
120+
sb.highlight('h1:contains("GitLab.com")')
121+
sb.highlight('button:contains("Sign in")')
122+
sb.driver.stop()
123+
```
124+
125+
> (Due to a change in Chrome 137 where the --load-extension switch was removed, one limitation with this format is that you can't load extensions directly. The other formats weren't affected by this change.)
126+
102127
--------
103128

104-
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_get_swag.py">test_get_swag.py</a>, which tests an e-commerce site:</p>
129+
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_get_swag.py">SeleniumBase/examples/test_get_swag.py</a>, which tests an e-commerce site:</p>
105130

106131
```python
107132
from seleniumbase import BaseCase
@@ -133,7 +158,7 @@ class MyTestClass(BaseCase):
133158
134159
--------
135160

136-
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_coffee_cart.py" target="_blank">test_coffee_cart.py</a>, which verifies an e-commerce site:</p>
161+
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_coffee_cart.py" target="_blank">SeleniumBase/examples/test_coffee_cart.py</a>, which verifies an e-commerce site:</p>
137162

138163
```zsh
139164
pytest test_coffee_cart.py --demo
@@ -147,7 +172,7 @@ pytest test_coffee_cart.py --demo
147172

148173
<a id="multiple_examples"></a>
149174

150-
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py" target="_blank">test_demo_site.py</a>, which covers several actions:</p>
175+
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py" target="_blank">SeleniumBase/examples/test_demo_site.py</a>, which covers several actions:</p>
151176

152177
```zsh
153178
pytest test_demo_site.py

examples/cdp_mode/ReadMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ sb.cdp.scroll_up(amount=25)
521521
sb.cdp.scroll_down(amount=25)
522522
sb.cdp.save_screenshot(name, folder=None, selector=None)
523523
sb.cdp.print_to_pdf(name, folder=None)
524+
sb.cdp.save_as_pdf(name, folder=None)
524525
```
525526

526527
--------

examples/raw_google.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
with SB(test=True, uc=True) as sb:
44
sb.open("https://google.com/ncr")
5-
sb.type('[title="Search"]', "SeleniumBase GitHub page\n")
6-
sb.sleep(1)
7-
sb.click('[href*="github.com/seleniumbase/"]')
8-
sb.save_screenshot_to_logs() # ./latest_logs/
5+
sb.type('[title="Search"]', "SeleniumBase GitHub page")
6+
sb.click("div:not([jsname]) > * > input")
97
print(sb.get_page_title())
8+
sb.sleep(2) # Wait for the "AI Overview" result
9+
if sb.is_text_visible("Generating"):
10+
sb.wait_for_text("AI Overview")
11+
sb.save_as_pdf_to_logs() # Saved to ./latest_logs/
12+
sb.save_page_source_to_logs()
13+
sb.save_screenshot_to_logs()

help_docs/method_summary.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ self.switch_to_driver(driver)
247247
self.switch_to_default_driver()
248248
self.save_screenshot(name, folder=None, selector=None, by="css selector")
249249
self.save_screenshot_to_logs(name=None, selector=None, by="css selector")
250+
self.save_as_pdf_to_logs(name=None)
250251
self.save_data_to_logs(data, file_name=None)
251252
self.append_data_to_logs(data, file_name=None)
252253
self.save_page_source(name, folder=None)
@@ -342,6 +343,8 @@ self.save_data_as(data, file_name, destination_folder=None)
342343
self.append_data_to_file(data, file_name, destination_folder=None)
343344
self.get_file_data(file_name, folder=None)
344345
self.print_to_pdf(name, folder=None)
346+
# Duplicates:
347+
# self.save_as_pdf(name, folder=None)
345348
self.get_downloads_folder()
346349
self.get_browser_downloads_folder()
347350
self.get_downloaded_files(regex=None, browser=False)

mkdocs_build/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pathspec==0.12.1
1414
Babel==2.17.0
1515
paginate==0.5.7
1616
mkdocs==1.6.1
17-
mkdocs-material==9.6.19
17+
mkdocs-material==9.6.20
1818
mkdocs-exclude-search==0.6.6
1919
mkdocs-simple-hooks==0.1.5
2020
mkdocs-material-extensions==1.3.1

sbase/steps.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,15 @@ def set_attributes(context, selector, attribute, value):
11911191
sb.set_attributes(selector, attribute, value)
11921192

11931193

1194+
@step("Save as PDF to logs")
1195+
@step("Save as PDF to the logs")
1196+
@step("User saves page as PDF to logs")
1197+
@step("User saves page as PDF to the logs")
1198+
def save_as_pdf_to_logs(context):
1199+
sb = context.sb
1200+
sb.save_as_pdf_to_logs()
1201+
1202+
11941203
@step("Save page source to logs")
11951204
@step("Save the page source to the logs")
11961205
@step("User saves page source to logs")

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.6"
2+
__version__ = "4.41.7"

seleniumbase/behave/behave_helper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ def generate_gherkin(srt_actions):
522522
)
523523
elif action[0] == "ss_tl":
524524
sb_actions.append("Save screenshot to logs")
525+
elif action[0] == "pdftl":
526+
sb_actions.append("Save as PDF to logs")
525527
elif action[0] == "spstl":
526528
sb_actions.append("Save page source to logs")
527529
elif action[0] == "sh_fc":

seleniumbase/core/browser_launcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
865865
cdp.scroll_down = CDPM.scroll_down
866866
cdp.save_screenshot = CDPM.save_screenshot
867867
cdp.print_to_pdf = CDPM.print_to_pdf
868+
cdp.save_as_pdf = CDPM.save_as_pdf
868869
cdp.page = page # async world
869870
cdp.driver = driver.cdp_base # async world
870871
cdp.tab = cdp.page # shortcut (original)

seleniumbase/core/recorder_helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ def generate_sbase_code(srt_actions):
556556
elif action[0] == "ss_tl":
557557
method = "save_screenshot_to_logs"
558558
sb_actions.append("self.%s()" % method)
559+
elif action[0] == "pdftl":
560+
method = "save_as_pdf_to_logs"
561+
sb_actions.append("self.%s()" % method)
559562
elif action[0] == "spstl":
560563
method = "save_page_source_to_logs"
561564
sb_actions.append("self.%s()" % method)

0 commit comments

Comments
 (0)