How to execute javascript with specified "type"?

25 views
Skip to first unread message

Zheng Chen

unread,
Jun 2, 2018, 11:04:45 PM6/2/18
to Selenium Users
See the following code, two pieces of javascript has "type". When I run the first piece of code by driver.ExecuteScript('MathJax.Hub.Config({TeX: { extensions: ["color.js","autoload-all.js"] }});'), it does not work. I am told the code need to be executed with "type" being "text/x-mathjax-config" when the page loads. How do I achieve this in selenium?

<!DOCTYPE html>
<html class="reftest-wait">
  <head>
    <title>colorbox</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        TeX: { extensions: ["color.js","autoload-all.js"] }
      });
    </script>
    <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  </head>

  <body>
    \( \colorbox {red} x y z \)
  </body>
</html>

David

unread,
Jun 4, 2018, 6:19:40 PM6/4/18
to Selenium Users
Interesting topic/scenario. Probably few people encounter this. I believe Selenium WebDriver specifically just executes type javascript when you execute (java)script from it but I may be mistaken, I'm unaware of any methods to explicitly specify type.

The Selenium WebDriver interface for javascript execution is like an alias to the browser's developer/javascript console in browser. So best way to figure out how you might be able to execute the code is if you can do it from the browser's developer/javascript console. If you can run the code there (w/o say having to switch/select the script type) then it should be runnable in Selenium WebDriver. That's how I would first test things out.

Your specific issue is more script specific, I would see if there's a MathJax forum and ask there how to execute/inject MathJax code from a browser javascript console (w/o specifying script type). W/o knowing MathJax, I'm assuming the type is actually some kind of modified type on top of javascript, unless it's its own script language exposed to browser via a browser plugin (not natively usable by browser).

At the moment though, there might be a way you can address this that just came to mind, using the javascript DOM, create a script tag element/object in javascript, set it's type attribute, and then set the value/content inside the tag to be the script code you want to execute (or have script code in external file URL and then just set src attribute to point to it), then add the script tag into the page's DOM (after page load likely) to execute the injected code. Then use Selenium WebDriver to execute this javascript you just defined. Sorry I don't have the time to craft an example, but you can search online how to create and add a script element into DOM.
Reply all
Reply to author
Forward
0 new messages