Firewatir: Firewatir dynamic drop down issue - ruby

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?

Related

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

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")

Sinatra dropdown list

Im glad you answered my question about my dev issue, I'll try to be more self-explanatory this time.
I have a main app.rb where I use several endpoints redirecting to my Sinatra Haml views.My project is about a Software Portfolio, so I have this class: Software, and Category, which relationship is: one software has one category, and a category has many softwares. In the form where you create a new Software entry, I put a dropdown list where you can choose between 3 different categories: Desktop, Web and App.
Until there, everything is going well. The thing is, when the Software list shows up, I want to put a dropdown list to filter by created categories (I already have the "add category" form with its Class) and I can't figure out how to add the filter within a Filter button in the software list form. Can you guys help me please? Of course I know how to put the button there, but I want to show only the software entries where the selected category matches. Here's the list form.
%select{:name => "category"}
%option Desktop
%option Web
%option Device
%input{:type => "submit", :value => "Filter", :class => "btn"}
%ul.list
- #sware.each do |software|
%div{:class =>"list-group"}
%a{:href =>"/software/edit/#{software.id}", :class =>"btn btn-lg btn-primary"}
= software.title
%a.pull-right(href="/software/delete/#{software.id}" class="btn btn-lg btn-danger") Delete
Thanks a lot in advance!
You're calling the index method like this:
post '/all' do
index(:category)
end
You're passing an argument to the index call, but the index method doesn't take any arguments.
Please include the full error with your question.
index action can be DRY'ed:
def index
category = case
when params[:Web] then :Web
when params[:Desktop] then :Desktop
when params[:Device] then :Device
end
#sware = Software.title.where(categorization: { Software.categorization => category })
end
"It just doesn't work" is not a good place to start investigation of the problem. More debug information is required.
Beside the point that #max pleaner made, you're not actually calling the right object in params. It should be params[:category] and you should be able to rewrite that much simpler:
get '/all' do
halt(401,'Not Authorized, please login to continue') unless session[:admin]
#sware = Software.all
haml :sware
end
post '/:category' do
#sware = Software.title.where(categorization: {Software.categorization => params[:category]}
haml :index # assuming index.haml is where you want to go
end
Then, assuming your file is properly indented, your Haml file should work as well:
%select{:name => 'category'}
%option Desktop
%option Web
%option Device
%input{:type => 'submit', :value => 'Filter', :class => 'btn'}
%ul.list
- #sware.each do |software|
%div{:class =>'list-group'}
%a{:href =>"/software/edit/#{software.id}", :class =>'btn btn-lg btn-primary'}
= software.title
%a.pull-right{:href=>"/software/delete/#{software.id}" :class=>'btn btn-lg btn-danger'} Delete
Of course, the more information you can provide, the better the problem can be understood.

Watir Error: unable to locate element

I am writing a web scraper with Watir and I can't seem to figure out this error I'm getting. I have an array of text for links on a certain page, but when I loop through it and click on the link and then go back, it breaks.
Here is the HTML code
<div>
Text here
</div>
<div>
Text here 2
</div>
<div>
Text here 3
</div>
And here is my Watir code
browser = Watir::Browser.new
browser.goto 'some_valid_site.com'
array = ['Text here', 'Text here 2', 'Text here 3']
array.each do |text|
browser.link(:text, text).click
browser.back
end
It executes the first link correctly, but when it comes to the second link, I get the following error message:
ruby-2.2.2#gemset/gems/watir-webdriver-0.8.0/lib/watir-webdriver/elements/element.rb:533:
in `assert_element_found': unable to locate element,
using {:element=>#<Selenium::WebDriver::Element:0x1bef61e9aef3c36a
id="{ad42ba23-8037-a745-8fd7-21955ab49406}">}
(Watir::Exception::UnknownObjectException)
I am pretty new to this so any advice would be much appreciated. Thanks!
Watir has a wait_until_present method:
browser = Watir::Browser.new
browser.goto 'some_valid_site.com'
array = ['Text here', 'Text here 2', 'Text here 3']
array.each do |text|
browser.link(:text, text).wait_until_present
browser.link(:text, text).click
browser.back
end
default wait time is 30 seconds which should be way more than enough. If the page isn't reloading on back you might need to use the explicit URL.

Insert value in a text field using Ruby - watir/selenium

I am trying to insert a number in the 'Mine Id' using Ruby - Watir/Selenium
Somehow the field <input> field is not being recognized as a text box and hence I am unable to enter values through the code.
The id 'inputdrs' is used multiple times on the same page.
Any suggestions how to achieve it.
The URL is this
http://www.msha.gov/drs/drshome.htm
The below don't work:
#browser.text_field(:id => /inputdrs/, :index => 2).set("3607277")
browser.text_field(:name, "inputdrs").set("3607277")
Thank you for your help
require 'watir-webdriver'
$browser = Watir::Browser.start "http://www.msha.gov/drs/drshome.htm"
a = 0
b = $browser.text_fields.length
while a < b
$browser.text_fields[a].set a
a += 1
end
This will put the value of a in each text field on this page. I REALLY drew out the loop so you can see whats going on. This isn't as dynamic as you would like it, but if the page has the same amount of text_fields you should be fine.
OR you can do something like..
$browser.text_field(:name => "MineId").select
$browser.send_keys "hello"
Problem
The html of the Mine ID field is:
<input size="8" maxlength="8" name="MineId" onclick="this.value='';" id="inputdrs" align="middle" type="number">
The line:
#browser.text_field(:id => /inputdrs/, :index => 2).set("3607277")
Will fail because it is inputting into the wrong text field. If you get all of the text fields with that id:
browser.text_fields(:id => 'inputdrs').collect(&:name)
#=> ["q", "MineId", "OperSearch", "MineName", "CntctrId", "CntCtrSearch", "Controller"]
You can see it is the second field. However, because Watir is using a 0-based index, you actually get the OperSearch field. This would have worked by using an :index => 1 instead.
The line:
browser.text_field(:name, "inputdrs").set("3607277")
Will fail because the "inputdrs" is the value of the id attribute and not the name attribute.
Solution
Given that the id attribute value is not unique for this page, you should probably not use it for locating. Instead, use something unique, such as the name attribute.
browser.text_field(:name => "MineId").set("3607277")

Look for text in a div that has multiple links under it with Watir

I am trying to verify that the text of a link is present in a . The div is not available until a link is clicked. After the link is click more links become available to click. Below is what the html looks like after the click to expose the other links.
<div class="more-links">
First Link
Second Link
Third Link
Fourth Link
Fifth Link
</div>
I want to find make sure the text "Third Link" is there after the div appears.
I have tried, but watir is saying that it cannot find that element.
assert_equal(true, browser.div(:class => "more-links").a(:text => "Third Link"))
I can find the text with this but is really slow and want to specify this particular div
browser.text.include?("Third Link")
Thoughts?
This following code isn't going to return true:
browser.div(:class => "more-links").a(:text => "Third Link")
#=> #<Watir::Link:0x..f99c8864c located=false specifiers={:tag_name=>["a"], :text=>"Third Link"}>
So, the assert_equal is returning false.
If exists? is appended to that line, then it returns true:
browser.div(:class => "more-links").a(:text => "Third Link").exists?
#=> true
browser.text.include?("Third Link")
I can find the text with this but is really slow and want to specify
this particular div
...which is exactly what you did here:
browser.div(:class => "more-links").a(:text => "Third Link")
So in your original line of code, you just need to replace 'browser' with your more specific search:
browser.div(:class => "more-links").a(:text => "Third Link")
Also note, that you would never write:
assert_equal(true, ...)
because you can save yourself some typing and just write:
assert(...)

Resources