@@ -1372,6 +1372,88 @@ def select_option_by_value(self, dropdown_selector, option,
1372
1372
dropdown_by = dropdown_by , option_by = "value" ,
1373
1373
timeout = timeout )
1374
1374
1375
+ def load_html_string (self , html_string , new_page = True ):
1376
+ """ Loads an HTML string into the web browser.
1377
+ If new_page==True, the page will switch to: "data:text/html,"
1378
+ If new_page==False, will load HTML into the current page. """
1379
+
1380
+ soup = self .get_beautiful_soup (html_string )
1381
+ scripts = soup .findAll ("script" )
1382
+ for script in scripts :
1383
+ html_string = html_string .replace (str (script ), "" )
1384
+ soup = self .get_beautiful_soup (html_string )
1385
+
1386
+ found_head = False
1387
+ found_body = False
1388
+ html_head = None
1389
+ html_body = None
1390
+ if soup .head and len (str (soup .head )) > 12 :
1391
+ found_head = True
1392
+ html_head = str (soup .head )
1393
+ html_head = re .escape (html_head )
1394
+ html_head = self .__escape_quotes_if_needed (html_head )
1395
+ html_head = html_head .replace ('\\ ' , ' ' )
1396
+ if soup .body and len (str (soup .body )) > 12 :
1397
+ found_body = True
1398
+ html_body = str (soup .body )
1399
+ html_body = re .escape (html_body )
1400
+ html_body = self .__escape_quotes_if_needed (html_body )
1401
+ html_body = html_body .replace ('\\ ' , ' ' )
1402
+ html_string = re .escape (html_string )
1403
+ html_string = self .__escape_quotes_if_needed (html_string )
1404
+ html_string = html_string .replace ('\\ ' , ' ' )
1405
+
1406
+ if new_page :
1407
+ self .open ("data:text/html," )
1408
+ inner_head = '''document.getElementsByTagName("head")[0].innerHTML'''
1409
+ inner_body = '''document.getElementsByTagName("body")[0].innerHTML'''
1410
+ if not found_body :
1411
+ self .execute_script (
1412
+ '''%s = \" %s\" ''' % (inner_body , html_string ))
1413
+ elif found_body and not found_head :
1414
+ self .execute_script (
1415
+ '''%s = \" %s\" ''' % (inner_body , html_body ))
1416
+ elif found_body and found_head :
1417
+ self .execute_script (
1418
+ '''%s = \" %s\" ''' % (inner_head , html_head ))
1419
+ self .execute_script (
1420
+ '''%s = \" %s\" ''' % (inner_body , html_body ))
1421
+ else :
1422
+ raise Exception ("Logic Error!" )
1423
+
1424
+ for script in scripts :
1425
+ js_code = script .string
1426
+ js_code_lines = js_code .split ('\n ' )
1427
+ new_lines = []
1428
+ for line in js_code_lines :
1429
+ line = line .strip ()
1430
+ new_lines .append (line )
1431
+ js_code = '\n ' .join (new_lines )
1432
+ js_utils .add_js_code (self .driver , js_code )
1433
+
1434
+ def load_html_file (self , html_file , new_page = True ):
1435
+ """ Loads a local html file into the browser from a relative file path.
1436
+ If new_page==True, the page will switch to: "data:text/html,"
1437
+ If new_page==False, will load HTML into the current page.
1438
+ Local images and other local src content WILL BE IGNORED. """
1439
+ if len (html_file ) < 6 or not html_file .endswith (".html" ):
1440
+ raise Exception ('Expecting a ".html" file!' )
1441
+ abs_path = os .path .abspath ('.' )
1442
+ file_path = abs_path + "/%s" % html_file
1443
+ f = open (file_path , 'r' )
1444
+ html_string = f .read ().strip ()
1445
+ f .close ()
1446
+ self .load_html_string (html_string , new_page )
1447
+
1448
+ def open_html_file (self , html_file ):
1449
+ """ Opens a local html file into the browser from a relative file path.
1450
+ The URL displayed in the web browser will start with "file://". """
1451
+ if len (html_file ) < 6 or not html_file .endswith (".html" ):
1452
+ raise Exception ('Expecting a ".html" file!' )
1453
+ abs_path = os .path .abspath ('.' )
1454
+ file_path = abs_path + "/%s" % html_file
1455
+ self .open ("file://" + file_path )
1456
+
1375
1457
def execute_script (self , script ):
1376
1458
return self .driver .execute_script (script )
1377
1459
@@ -2610,6 +2692,9 @@ def add_css_style(self, css_style):
2610
2692
def add_js_code_from_link (self , js_link ):
2611
2693
js_utils .add_js_code_from_link (self .driver , js_link )
2612
2694
2695
+ def add_js_code (self , js_code ):
2696
+ js_utils .add_js_code (self .driver , js_code )
2697
+
2613
2698
def add_meta_tag (self , http_equiv = None , content = None ):
2614
2699
js_utils .add_meta_tag (
2615
2700
self .driver , http_equiv = http_equiv , content = content )
0 commit comments