简体   繁体   中英

How to Add a <script> into Head Using Selenium's JavascriptExecutor

Summary

I want to figure out a way to add a <script> tag into the head of DOM using Selenium 's JavascriptExecutor , or any other way of doing this would be nice.


I have tried many ways and also found a few similar topics and none of them solved my problem which is why I felt the need to ask it on here.

For example :

Suggested solutions in this question did not solve my problem. Some people say it worked for them but nope, it didn't for me.


What I've been trying to execute?

Here is the small snippet of the code that I want to execute:

            WebDriver driver = new FirefoxDriver();
            JavascriptExecutor jse = (JavascriptExecutor) driver;

            jse.executeScript("var s = document.createElement('script');");
            jse.executeScript("s.type = 'text/javascript';");
            jse.executeScript("s.text = 'function foo() {console.log('foo')}';");
            jse.executeScript("window.document.head.appendChild(s);");

I just skipped the code above where you navigate to a webpage using driver.get() etc. and then try to execute the scripts.

Also, s.text would contain the actual script that I want to use so I just put there a foo() function just to give the idea.

The above code throws this error when you run it:

Exception in thread "main" org.openqa.selenium.JavascriptException: ReferenceError: s is not defined


So far I've tried every possible solution I could find on the Internet but none of them seems to work.

OP came up with the following solution:

jse.executeScript("var s=window.document.createElement('script');" + 
"s.type = 'text/javascript';" + "s.text = function foo() {console.log('foo')};" + 
"window.document.head.appendChild(s);");

For one, this line is invalid.

    jse.executeScript("s.text = 'function foo() {console.log('foo')}';");

Note how you wrap single-quote text in single quotes. Use one set as "\\""

I would personally do this by doing (edited to make it a global function):

using OpenQA.Selenium.Support.Extensions;

driver.ExecuteJavascript("window.foo = function foo() {console.log('foo')}");

It's as simple as that. You are registering foo as a method by doing this. After you execute this javascript, you can manually go in to the browser developer tools and call "foo()" to check. Additionally, you can check this by registering it directly in the console. Just enter "function foo() {console.log('foo')}" into your browser console, and then call "foo()".

No need to add this as a script tag.

EDIT #2: I fixed my above code suggestion so that the method is assigned to the window, and thus accessible globally, and outside of the anonymous script that javascript executor runs the code in. The original issues with this not working are resolved by this, at least in my testing of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM