python - Selenium: How to Inject/execute a Javascript in to a Page before loading/executing any other scripts of the page? -


i'm using selenium python webdriver in order browse pages. want inject javascript code in pages before other javascript codes loaded , executed. on other hand, need js code executed first js code of page. there way selenium?

i googled couple of hours, couldn't find proper answer!

if cannot modify page content, may use proxy, or use content script in extension installed in browser. doing within selenium write code injects script 1 of children of existing element, won't able have run before page loaded (when driver's get() call returns.)

string name = (string) ((javascriptexecutor) driver).executescript(     "(function () { ... })();" ... 

the documentation leaves unspecified moment @ code start executing. want before dom starts loading guarantee might satisfiable proxy or extension content script route.

if can instrument page minimal harness, may detect presence of special url query parameter , load additional content, need using inline script. pseudocode:

 <html>     <head>        <script type="text/javascript">        (function () {        if (location && location.href && location.href.indexof("selenium_test") >= 0) {           var injectscript = document.createelement("script");           injectscript.setattribute("type", "text/javascript");            //another option perform synchronous xhr , inject via innertext.           injectscript.setattribute("src", url_of_extra_script);           document.documentelement.appendchild(injectscript);            //optional. cleaner remove. has been loaded @ point.           document.documentelement.removechild(injectscript);        }        })();        </script>     ... 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -