Watir::Exception::UnknownObjectException: unable to locate element - ruby

This is the drop-down from where I will select the various contract type
This the DIV, UL and LI classes which I have used in the code
irb(main):128:0> li_count_in_ul9 = browser.div(:class => "select2-drop select2-display-none select2-with-searchbox select2-drop-active").ul(:class => "select2-results").lis(:class => "select2-results-dept-0 select2-result select2-result-selectable")
=> #<Watir::LICollection:0x000000028ac0d0 #parent=#<Watir::UList:0xfc812aa2 located=false selector={:class=>"select2-results", :tag_name=>"ul"}>, #selector={:class=>"select2-results-dept-0 select2-result select2-result-selectable", :tag_name=>"li"}>
irb(main):129:0> li_count_in_ul9[0].click
Watir::Exception::UnknownObjectException: unable to locate element, using {:class=>"select2-results-dept-0 select2-result select2-result-selectable", :tag_name=>"li", :index=>0}
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:508:in `assert_exists'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:114:in `click'
from (irb):129
from C:/Ruby23-x64/bin/irb.cmd:19:in `<main>'
irb(main):130:0> sleep 5
=> 5
irb(main):131:0>

The problem appears to be with the:
browser.div(:class => "select2-drop select2-display-none select2-with-searchbox select2-drop-active")
From the image of the HTML, that is not the div that contains the li elements. It is a sibling div that contains the elements. As it has an id, you could locate it via:
browser.div(:id => "select2-drop")
Then end result being:
li_count_in_ul9 = browser.div(:id => "select2-drop").ul(:class => "select2-results").lis(:class => "select2-results-dept-0 select2-result select2-result-selectable")
From the HTML shared, this might be over specified. You may simply be able to do:
li_count_in_ul9 = browser.div(:id => "select2-drop").lis(:class => "select2-result-selectable")

Related

Capybara can't find svg element when using ID

The following element is on the page:
<use xlink:href="pending">
<svg id="pending">
Using this: page.should have_css('svg', :id => 'pending', :count => 1)
Or this: page.should have_css('svg[id="pending"]', :count => 1)
Is returning no matches.
I have tried: page.should have_css('svg', :count => 1)
Which does return matches, but what i need to find is the specific element with the ID of "pending".
How about using has_selector? Since your ID's should be unique across the whole document, no count should be necessary.
page.should have_selector('#pending')
has_selector? docs

How to get Span collection by id in Watir

Is there a method in watir to get list of all the span collection by id. I can traverse the span tags only by index and one item at a time.
i want to get the entire collection of the span tags and loop through it.
irb(main):060:0> ie.span(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl132_lblSubject/).text
=> "30 stuff"
irb(main):065:0> ie.span(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl/).exists?
=> true
irb(main):073:0> spans=ie.span(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl/ ,:index=>3)
=> #<Watir::Span: located: false; {:id=>/ctl00_ContentPlaceHolder1_DataList1_ctl/, :index=>3, :tag_name=>"span"}>
irb(main):074:0> spans.text
=> "Well, morning."
irb(main):075:0>
Yes, you can iterate. Write the following code
ie.spans(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl/).each do |span|
p span.text
end
It will print all the span's text which is matching your id.

Why do I get invalid attribute error when I pass in a css selector?

stackoverflow,
Here's what I'm trying to do
def get_element_from_list(root, item, index)
#browser.elements(:css => root).each do |element|
if element.present?
return element.element(:css => item, :index => index)
end
end
raise Selenium::WebDriver::Error::NoSuchElementError
end
get_element_from_list('div[class*=x-combo-list]', 'x-combo-list-item', index).click
gives me Watir::Exception::MissingWayOfFindingObjectException: invalid attribute: :css
What I don't understand is if I simply do
#browser.elements(:css => 'div[class*=x-combo-list]').each do |element|
if element.present?
return element.element(:css => 'x-combo-list-item', :index => index)
end
end
basically replacing root and item with the actual strings it works without error.
I think there might be a bug that prevents locating elements with the :css and :index locator - Issue 241.
You can work around the issue by getting an element collection and then getting the element at the specific index:
return element.elements(:css => 'x-combo-list-item')[index]
(Note that I think this css-selector might be wrong. It is probably meant to be .x-combo-list-item.)
Alternatively, assuming that x-combo-list-item is actually the element's class, you could do:
return element.element(:class => 'x-combo-list-item', :index => index)

Using middleman, how do you include one HAML file in another HAML file?

I'm using middleman to do some rapid prototyping and can't for the life of me figure out how to include one HAML file into another HAML file.
I can include stuff in a layout file, but can't get one non-layout file to include another non-layout file. There are blocks of HTML that I want to reuse on some pages and I think I could do this. I've tried:
- render: partial=>"shared/nav.haml"
=shared/nav.html
="shared/nav.html
and none of these work.
Am I missing a config option or plugin? This is a fresh middleman install.
ANSWER
Partials may need file names that start with an underscore. My partial is placed in a folder called shared. The full name of the file is _nav.html.haml
This worked for me.
!= haml :"shared/_nav"
Example in context:
#email.main.subscriber.resize
#bg-wrap
%div
%img{:src=>"images/backgrounds/image.png",:alt=>""}
%section#zone10
!= haml :"shared/_nav"
You may also use the format specified in the approved answer below.
I've been using HAML with MiddleMan and couldn't be happier. Here is what is working for me:
I have a file: source/_donate_buttons.h
#DonationButtons
%p= t('searching.donate_cover_costs')
%br
= partial(:paypal_donate_button, :locals => {:amount => 1,
:amount_text => t('searching.donate_1')})
This uses the partial statement shown to include a file called source/_paypal_donate_button.html.haml.
And I include the _donate_buttons.html.haml file itself in a couple of places with:
= partial "donate_buttons"
though I think this could also be:
= partial :donate_buttons
I.e. I think partial is the magic you're looking for.
And, just for completeness, here is a slightly stripped down _paypal_donate_button.haml which shows how the paramaterization works there:
-btnclass = (locals.key?(:highlight) && locals[:highlight] ? "HighlightedDonationButton" : "DonationButton")
-btnstyle = locals.key?(:button_style) && locals[:button_style]
.DonationButtonContainer
%form{:action => "https://www.paypal.com/cgi-bin/webscr", :method => "post"}
%input{:name => "business", :type => "hidden", :value => "payments#example.com"}
%input{:name => "cmd", :type => "hidden", :value => "_donations"}
%input{:name => "amount", :type => "hidden", :value => "#{amount}.00"}
%input{:name => "currency_code", :type => "hidden", :value => "USD"}
%input{:class => btnclass, :alt => t('paypal.alt_text'),
:style => "cursor: pointer; font-size: 18px; #{btnstyle}", :type => "submit", :value => amount_text}
Fwiw, I don't think the file needs to be _filename.html.haml and can instead be _filename.haml. Also, I'm localizing these, so ignore the t('tagname') and just put strings there. (I didn't want to introduce an error copy-pasting the examples so I left them in there.)
Hope this helps!

Firewatir: Firewatir dynamic drop down issue

I am having any issue with selecting any item from the drop down. Below is the HTML from our site. The HTML looks like this
<div class="x-form-field-wrap x-trigger-wrap-focus"
id="ext-gen157" style="width: 170px;"><input type="hidden"
id="parentEntity" name="parentEntity" value=""><input type="text"
id="cmbParentEntityId" autocomplete="off" size="24" class="
x-form-text x-form-field x-form-focus" style="width: 145px;">
<img class="x-form-trigger x-form-arrow-trigger"
src="../ext/resources/images/default/s.gif" id="ext-gen158"></div>
So I have created a watir code which looks like this:
#browser.text_field(:id,"cmbParentEntityId").set("1")
which search for all the accounts starting with 1.Once the value is set to 1, the drop down is showing only accounts starting with 1. Below is the HTML code from the drop down
<div class="x-combo-list-inner" id="ext-gen336" style="width:
248px; overflow: auto; height: 40px;"><div class="x-combo-list-item
x-combo-selected">10_12_2010</div><div
class="x-combo-list-item ">10_13_2010</div></div>
Based on the above code I have created the Watir code
#browser.div(:class => "x-combo-list-inner", :text => "10_12_2010").click
But nothing is happening, I have searched the web but couldn't find any answers, I really appreciate that if anyone can help me to point to right direction.
Thanks
What do you mean that nothing is happening? If i try the code provided by you, then i'll get an expected UnknownObjectException:
irb(main):003:0> b.div(:class => "x-combo-list-inner", :text => "10_12_2010").click
Watir::Exception::UnknownObjectException: Unable to locate element, using {:class=>"x-combo-list-inner", :text=>"10_12_2010"}
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:57:in `assert_exists'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:315:in `enabled?'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:62:in `assert_enabled'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:259:in `click!'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:229:in `click'
from (irb):3
That is because you're trying to find a div element with a class of "x-combo-list-inner" and a text of "10_12_2010". There isn't such an element. See this:
irb(main):007:0> b.div(:class => "x-combo-list-inner").text
=> "10_12_2010\r\n10_13_2010"
Text of "x-combo-list-inner" includes texts for every child element. You could search for that particular child element like this:
irb(main):008:0> b.div(:class => "x-combo-list-inner").div(:text => "10_12_2010").html
=> "\r\n<DIV class=\"x-combo-list-item
x-combo-selected\">10_12_2010</DIV>"
Or with regexps:
irb(main):009:0> b.div(:class => "x-combo-list-inner", :text => /10_12_2010/).text
=> "10_12_2010\r\n10_13_2010"
And when it comes to clicking then you have to know which exact div you need to click - is it the first one, or the second one. Also, if nothing happens then you have to find out what JavaScript events are binded to these elements exactly and then fire events manually:
irb(main):010:0> div = b.div(:class => "x-combo-list-inner").div(:text => "10_12_2010")
=> #<Watir::Div:0x5846088 located=false how={:text=>"10_12_2010"} what=nil>
irb(main):013:0> div.fire_event("onmousedown")
=> nil
irb(main):014:0> div.fire_event("onmouseup")
=> nil
My guess is that some JavaScript event should be explicitly fired. See How to find out which JavaScript events fired?

Resources