How to execute javascript with specified "type"?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to execute javascript with specified "type"?

Zheng Chen
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>

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/17ef2482-6042-4882-9d48-60d7ee856ed9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to execute javascript with specified "type"?

David
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.

On Saturday, June 2, 2018 at 8:04:45 PM UTC-7, Zheng Chen wrote:
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="<a href="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fcdn.mathjax.org%2Fmathjax%2Flatest%2FMathJax.js%3Fconfig%3DTeX-AMS-MML_HTMLorMML\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFgw0tcNC_v7TGW9eha40NqkdpFRg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fcdn.mathjax.org%2Fmathjax%2Flatest%2FMathJax.js%3Fconfig%3DTeX-AMS-MML_HTMLorMML\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFgw0tcNC_v7TGW9eha40NqkdpFRg&#39;;return true;">https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  </head>

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

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/30fde7cf-7e7d-4a69-abc6-3c1d5f0671b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.