Skip to content

Commit 34b114f

Browse files
committed
Add the save_page_source(file_name) method
1 parent 8dc34a1 commit 34b114f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,15 @@ def save_screenshot(self, name, folder=None):
14681468
""" The screenshot will be in PNG format. """
14691469
return page_actions.save_screenshot(self.driver, name, folder)
14701470

1471+
def save_page_source(self, name, folder=None):
1472+
""" Saves the page HTML to the current directory (or given subfolder).
1473+
If the folder specified doesn't exist, it will get created.
1474+
@Params
1475+
name - The file name to save the current page's HTML to.
1476+
folder - The folder to save the file to. (Default = current folder)
1477+
"""
1478+
return page_actions.save_page_source(self.driver, name, folder)
1479+
14711480
def wait_for_ready_state_complete(self, timeout=None):
14721481
try:
14731482
# If there's an alert, skip

seleniumbase/fixtures/page_actions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from selenium.webdriver.remote.errorhandler import NoSuchFrameException
3333
from selenium.webdriver.remote.errorhandler import NoSuchWindowException
3434
from seleniumbase.config import settings
35+
from seleniumbase.core import log_helper
3536

3637

3738
def is_element_present(driver, selector, by=By.CSS_SELECTOR):
@@ -488,6 +489,32 @@ def save_screenshot(driver, name, folder=None):
488489
pass
489490

490491

492+
def save_page_source(driver, name, folder=None):
493+
"""
494+
Saves the page HTML to the current directory (or given subfolder).
495+
If the folder specified doesn't exist, it will get created.
496+
@Params
497+
name - The file name to save the current page's HTML to.
498+
folder - The folder to save the file to. (Default = current folder)
499+
"""
500+
if "." not in name:
501+
name = name + ".html"
502+
if folder:
503+
abs_path = os.path.abspath('.')
504+
file_path = abs_path + "/%s" % folder
505+
if not os.path.exists(file_path):
506+
os.makedirs(file_path)
507+
html_file_path = "%s/%s" % (file_path, name)
508+
else:
509+
html_file_path = name
510+
page_source = driver.page_source
511+
html_file = codecs.open(html_file_path, "w+", "utf-8")
512+
rendered_source = log_helper.get_html_source_with_base_href(
513+
driver, page_source)
514+
html_file.write(rendered_source)
515+
html_file.close()
516+
517+
491518
def _get_last_page(driver):
492519
try:
493520
last_page = driver.current_url

0 commit comments

Comments
 (0)