1

So there are several posts on how to query the dom to find an element by xpath. I found a post, Is there a way to get element by XPath using JavaScript in Selenium WebDriver?, that mostly meets my needs. My problem is stemming from the fact that the element I am trying to locate is a script, and that script needs to be loaded asynchronously. That seems to break finding its path using

var path = "//script[contains (@src, 'locationOfScript')]";

I think the root cause may be Chrome delaying the async script load until after the page is complete: Chrome delays load of script with async attribute.

I'm developing a userscript in Chrome, and must leave the script as async, otherwise I'd just drop the async attribute.

Any way to query the document for a script that won't load until after everything else?

Thanks!

Community
  • 1
  • 1
Heath Carroll
  • 323
  • 2
  • 12

1 Answers1

0

The async attribute will cause the script not to block so you cannot be sure when it will load. However, you can be sure when the DOM is done being loaded with the following code:

document.onreadystatechange = function () {
    if (document.readyState === "complete") {
        var path = "//script[@src='locationOfScript']";
        // etc...
    }
}
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • You beat me to it :) While this seems to work on my simple test page, it comes back null in production. Odd. Still poking. – Heath Carroll May 04 '15 at 00:36
  • I think I found the problem. The script being referenced is a coffeescript, not core js. So your solution works for the question asked. Thanks! – Heath Carroll May 04 '15 at 01:14