Sam
Sam

Reputation: 1253

Why doesn't Watir find script tag by src attribute

Navigating to any page, I can review the scripts present with Watir:

b = Watir::Browser.new
b.goto "https://macys.com"
b.scripts.collect(&:src)
# => [ ..., "https://mathid.mathtag.com/d/i.js", ...]

The result is a large array of scripts, I've chosen one at random to find:

b.script(src: "https://mathid.mathtag.com/d/i.js").html
# => Watir::Exception::UnknownObjectException: timed out after 30 seconds, waiting for #<Watir::HTMLElement: located: false; {:xpath=>"//script[@src='https://mathid.mathtag.com/d/i.js']"}> to be located; Maybe look in an iframe?
# ...

But it never finds any script this way. Why? How can I detect the presence without investigating the scripts.collect(&:src)?

Upvotes: 0

Views: 264

Answers (2)

titusfortner
titusfortner

Reputation: 4194

Watir is locating the element based on string matching what is present in the DOM. If you investigate the DOM, you'll see that the https: is not present, just the //mathid.mathtag.com/d/i.js. The browser driver translates that when queried about the attribute value to include the https, but Watir has no way to assume that when locating.

This will work:

browser.script(src: '//mathid.mathtag.com/d/i.js')

Upvotes: 2

Rajagopalan
Rajagopalan

Reputation: 6064

It's because the link you are trying doesn't have https:, The element you are trying to locate is this,

<script type="text/javascript" async="" src="//mathid.mathtag.com/d/i.js"/>

So write this code, it would work.

p b.script(src: "//mathid.mathtag.com/d/i.js").html

Upvotes: 1

Related Questions