Skip to content

Commit 770f10f

Browse files
committed
Optimize JS by putting html <script> tags before the end of <body>
1 parent b853569 commit 770f10f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

seleniumbase/fixtures/js_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,14 @@ def add_css_link(driver, css_link):
307307
def add_js_link(driver, js_link):
308308
script_to_add_js = (
309309
"""function injectJS(link) {
310-
var head = document.getElementsByTagName("head")[0];
310+
var body = document.getElementsByTagName("body")[0];
311311
var script = document.createElement("script");
312312
script.src = link;
313313
script.defer;
314314
script.type="text/javascript";
315315
script.crossorigin = "anonymous";
316316
script.onload = function() { null };
317-
head.appendChild(script);
317+
body.appendChild(script);
318318
}
319319
injectJS("%s");""")
320320
js_link = escape_quotes_if_needed(js_link)
@@ -341,12 +341,12 @@ def add_js_code_from_link(driver, js_link):
341341
js_link = "http:" + js_link
342342
js_code = requests.get(js_link).text
343343
add_js_code_script = (
344-
'''var h = document.getElementsByTagName('head').item(0);'''
345-
'''var s = document.createElement("script");'''
346-
'''s.type = "text/javascript";'''
347-
'''s.onload = function() { null };'''
348-
'''s.appendChild(document.createTextNode("%s"));'''
349-
'''h.appendChild(s);''')
344+
'''var body = document.getElementsByTagName('body').item(0);'''
345+
'''var script = document.createElement("script");'''
346+
'''script.type = "text/javascript";'''
347+
'''script.onload = function() { null };'''
348+
'''script.appendChild(document.createTextNode("%s"));'''
349+
'''body.appendChild(script);''')
350350
js_code = js_code.replace('\n', '')
351351
js_code = escape_quotes_if_needed(js_code)
352352
driver.execute_script(add_js_code_script % js_code)

0 commit comments

Comments
 (0)