Skip to content

Commit 375cfc7

Browse files
committed
Update code for injecting html strings into pages
1 parent 384e02e commit 375cfc7

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,34 @@ def load_html_string(self, html_string, new_page=True):
13601360
If new_page==True, the page will switch to: "data:text/html,"
13611361
If new_page==False, will load HTML into the current page. """
13621362

1363+
soup = self.get_beautiful_soup(html_string)
1364+
found_base = False
1365+
links = soup.findAll("link")
1366+
href = None
1367+
1368+
for link in links:
1369+
if link.get("rel") == ["canonical"] and link.get("href"):
1370+
found_base = True
1371+
href = link.get("href")
1372+
href = self.get_domain_url(href)
1373+
if found_base and html_string.count("<head>") == 1 and (
1374+
html_string.count("<base") == 0):
1375+
html_string = html_string.replace(
1376+
"<head>", '<head><base href="%s">' % href)
1377+
elif not found_base:
1378+
bases = soup.findAll("base")
1379+
for base in bases:
1380+
if base.get("href"):
1381+
href = base.get("href")
1382+
if href:
1383+
html_string = html_string.replace(
1384+
'base: "."', 'base: "%s"' % href)
1385+
13631386
soup = self.get_beautiful_soup(html_string)
13641387
scripts = soup.findAll("script")
13651388
for script in scripts:
1366-
html_string = html_string.replace(str(script), "")
1389+
if script.get("type") != "application/json":
1390+
html_string = html_string.replace(str(script), "")
13671391
soup = self.get_beautiful_soup(html_string)
13681392

13691393
found_head = False
@@ -1413,19 +1437,28 @@ def load_html_string(self, html_string, new_page=True):
14131437

14141438
for script in scripts:
14151439
js_code = script.string
1416-
js_code_lines = js_code.split('\n')
1417-
new_lines = []
1418-
for line in js_code_lines:
1419-
line = line.strip()
1420-
new_lines.append(line)
1421-
js_code = '\n'.join(new_lines)
1422-
js_utils.add_js_code(self.driver, js_code)
1440+
js_src = script.get("src")
1441+
if js_code and script.get("type") != "application/json":
1442+
js_code_lines = js_code.split('\n')
1443+
new_lines = []
1444+
for line in js_code_lines:
1445+
line = line.strip()
1446+
new_lines.append(line)
1447+
js_code = '\n'.join(new_lines)
1448+
js_utils.add_js_code(self.driver, js_code)
1449+
elif js_src:
1450+
js_utils.add_js_link(self.driver, js_src)
1451+
else:
1452+
pass
14231453

14241454
def load_html_file(self, html_file, new_page=True):
14251455
""" Loads a local html file into the browser from a relative file path.
14261456
If new_page==True, the page will switch to: "data:text/html,"
14271457
If new_page==False, will load HTML into the current page.
14281458
Local images and other local src content WILL BE IGNORED. """
1459+
if self.__looks_like_a_page_url(html_file):
1460+
self.open(html_file)
1461+
return
14291462
if len(html_file) < 6 or not html_file.endswith(".html"):
14301463
raise Exception('Expecting a ".html" file!')
14311464
abs_path = os.path.abspath('.')

0 commit comments

Comments
 (0)