1
votes

I'm having a really strange issue with watir-webdriver.

Here's a snapshot of the input tag I'm trying to reach (couldn't figure out a way to get the source after the javascripts created the popup, lol)

HTML of the input tag Anyway here's some of my code that uses xpath to locate these elements (there is two text fields and a select tag)

firstname = b.element(:xpath, "//div[@class='ap_popover']/input[@name='firstName']")
lastname =  b.element(:xpath, "//div[@class='ap_popover']/input[@name='lastName']")
authorselector = b.element(:xpath, "//div[@class='ap_popover']/select")
puts firstname
puts lastname
puts authorselector

This code successfully returns the watir element objects. However when I try to cast them:

puts firstname.to_subtype

it freaks out:

C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.4.1/lib/watir-webdriver/elements/element.rb:262:in `assert_exists': unable to locate element, using {:xpath=>"//div[@class='ap_popover']/input[@name='lastName']"} (Watir::Exception::UnknownObjectException)

So, what's going on? It can find them via xpath no problem but then when I try to cast them all of a sudden xpath search fails?

It's worth mentioning the html I'm perusing through is created in it's entirety by javascript, hence why I couldn't just copy\paste it here and had to take a screenshot.

Thanks!

1
Instead of doing element --> subtype. Can you access it directly b.textfield(:name => 'firstName') . I've been having some unable to located element problems recently with watirwebdriver...Kassym Dorsel
It can't find that exact one. There is another one on the page that serves as a 'prototype' for the javascript to load - but it's permanently hidden and you can't (and aren't supposed to) interact with it. So yeah it finds a textfield with that name but it finds the wrong one. It's as if it doesn't realize the html has been augmented.dsp_099
In that case b.text_fields(:name => 'firstName') should return both of them and you can select the second one.Kassym Dorsel
Why are you trying to cast an input element in the first place?Chuck van der Linden

1 Answers

4
votes

xpath is evil avoid it if at all possible. it's too easy to make mistakes, hard to read, and generally slower.

Have you tried something like

b.div(:id => 'contributors-table').textfield(:name => 'firstName')

If you have some wacky invalid HTML where they have two copies of all this stuff (and thus duplicated ID values which is not valid for HTML standard) then you can add in the INDEX of the element, which in this case might be needed both for the div container, and then maybe also for the input field if there are more than one of them.

b.divs(id => 'contributors-table').size  #how many are there?

#example, second instance of the contributors table, third instance in that table of an text input field with the name 'firstName'
b.div(:id => 'contributors-table', :index => 1).textfield(:name => 'firstName', :index => 2)