Robot framework select variable from dropdown - xpath

I am trying to select an item from a dropdown list in robot framework (using RIDE), but I cannot get the item by variable name.
<div class="chosen-drop">
<div class="chosen-search">
<input type="text" autocomplete="off">
</div>
<ul class="chosen-results">
<li class="active-result" data-option-array-index="0">Geen optie gekozen</li>
<li class="active-result" data-option-array-index="2">ABB</li>
<li class="active-result" data-option-array-index="3">Algem</li>
<li class="active-result" data-option-array-index="4">AOV</li>
<li class="active-result" data-option-array-index="5">AW</li>
<li class="active-result" data-option-array-index="8">AOZ</li>
</ul>
</div>
I can use this and get the result:
Click Element xpath=//*[#id="KEUZE_N_MiddelId_N1010D_chosen"]
Click Element xpath=//*
[#id="KEUZE_N_MiddelId_N1010D_chosen"]/div/ul/li[4]
But the index number can change, so I want to click the element based on the value, in this example 'ABB'. How can I achieve this?

You can Try the following:
Select From List By Label| css=ul.chosen-results| ABB

It is very similar to this SO post but not exact enough to be considered a duplicate. Based on your already achieved results I think this should work for you.
[#id="KEUZE_N_MiddelId_N1010D_chosen"]/div/ul/li[text() = 'ABB']

Related

Dynamic menu and links

I have a navigation:
Cars->Car 1->Menu
Where Car 1 is a dynamic value. I choose a Car X, and I get a menu for Car X. How to do this?
I mean that when choosing Car X, in links was added value Car=x.
cars->car_id->maintenances->index
cars->car_id->maintenances->maintenance_id->edit
cars->car_id->repairs->index
cars->car_id->repairs->repair_id->edit
Please help. Sorry for my english.
I don't know your tables fields but I guess you could do something like this:
<ul>
#foreach($cars as $car)
<li>
{!! $car->name!!}
<ul class="submenu">
#foreach($car->meintenances as $meintenance)
<li>
{!!$meintenance->name!!}
</li>
#endforeach
</ul>
</li>
#enforeach
</ul>
Same for repairs

Trouble selecting quantity out of drop down in watir

I have never run into an issue with being able to select a value in a drop down, so I'm not sure how this is different. First, here is the HTML I'm working with:
<div class="hn-select-content hn-select-expand" ng-class="{'open-to-left':openToLeft, 'expand-to-left':expandToLeft}" ng-transclude="" style="">
<ul class="qty-discount whiteBackground border border-hn-secondary-lt text-small ng-scope" scroll-lock="">
<li float-container="">
<div class="float-cont">
<ul class="text-small">
<li class="selected ng-isolate-scope HN-Item-Opt-Sel" li="" <="" on-option-select="changeQuantity(val)" value="1" option="1" ng-class="{'HN-Item-Opt-Sel selected':atcData.quantity == 1}" hn-select-option="">
<div class="hn-select-option ng-binding">1</div>
</li>
</ul>
</li>
<li float-container="">
<div class="text-hn-red float-cont">
<div scroller="qty-discount" floater="" style="">
<span> 10% Off </span>
</div>
<div></div>
</div>
<ul class="text-small">
<li class="ng-isolate-scope" li="" <="" on-option-select="changeQuantity(val)" value="2" option="2" ng-class="{'HN-Item-Opt-Sel selected':atcData.quantity == 2}" hn-select-option="">
<div class="hn-select-option ng-binding">2</div>
</li>
I want to select a specific option. in this case, I have a variable set for the quantity, and it's set to 2.
This it the code from the step that is failing:
#browser.div(:id, 'hn_modal_contentIV').div(:text, '1').when_present.click
#browser.ul(:class, 'whiteBackground border border-hn-secondary-lt text-small ng-scope').div(:text, quantity).when_present(5).click
#browser.span(:class, 'redText floatRight marginTopOnly3px').wait_until_present(10)
And this is the error I receive:
Watir::Wait::TimeoutError: timed out after 5 seconds, waiting for {:text=>"2", :tag_name=>"div"} to become present
The drop down box opens, I just can't get the value to be selected.
I have tried a few other variations, but none have worked. Any help would be GREATLY appreciated. Hopefully there is something small that I'm just missing.
Thanks!
First, before this code, HTML must have an select list, perhaps this select list have the attribute "display: none", then you can:
browser.execute_script("document.getElementById('[id of this select list ]').style.display = 'block';")
browser.select(:id => '[id of this select]').option(:text => '[text that you need]').select

Select visible xpath in list

I am trying to get the error message off of a page from a site. The list contains several possible errors so i can't check by id; but I do know that the one with display:list-item is the one I want. This is my rule but doesn't seem to work, what is wrong with it? What I want returned is the error text in the element.
//*[#id='errors']/ul/li[contains(#style,'display:list-item')]
Example dom elements:
<div id="errors" class="some class" style="display: block;">
<div class="some other class"></div>
<div class="some other class 2">
<span class="displayError">Please correct the errors listed in red below:</span>
<ul>
<li style="display:none;" id="invalidId">Enter a valid id</li>
<li style="display:list-item;" id="genericError">Something bad happened</li>
<li style="display:none;" id="somethingBlah" ............ </li>
....
</ul>
</div>
The correct XPath should be:
//*[#id='errors']//ul/li[contains(#style,'display:list-item')]
After //*[#id='errors'] you need an extra /, because <ul> is not directly beneath it. Using // again scans all underlying elements for <ul>.
If you are capable to not use // it would be better and faster and less consuming.

Select elements which has certain descendent using Xpath

I want to Select all the LI elements which contain SPAN with id="liveDeal152_dealPrice" as descendents. How do i do this with xpath?
Here is a sample html
<ul>
<li id="liveDeal_152">
<p class="price">
<em>|
<span class="WebRupee">₹ </span>
<span id="liveDeal152_dealPrice">495 </span>
</p>
</li>
<li id="liveDeal_152">
<p class="price">
<em>|
<span class="WebRupee">₹ </span>
(price hidden)
</p>
</li>
</ul>
//li[.//span[#id = 'liveDeal152_dealPrice']] should do. Or more verbose but closer to your textual description //li[descendant::span[#id = 'liveDeal152_dealPrice']].
Use this
//li[.//span[#id="liveDeal152_dealPrice"]]
It selects
ALL <li> ELEMENTS
//li[ ]
THAT HAVE A <span> DESCENDANT
.//span[ ]
WITH id ATTRIBUTE EQUAL TO "liveDeal152_dealPrice"
#id="liveDeal152_dealPrice"
That said, it doesn't seem like a very wise element selection, mostly due to the dynamically looking id. If you're going to use it once, it's probably ok, but if you're using it, say, for testing and will reuse it many times, it might cause trouble. Are you sure this won't change when you change your website and/or database?
As a side note:
ul stands for "unordered list"
ol stands for "ordered list"
li stands for "list item"

How can I select the 6th option in the list?

<div id="suggestionlist">
<ol id="suggestionroot">
<li id="sugg_1">
<li id="sugg_2">
<li id="sugg_3">
<li id="sugg_4">
<li id="sugg_5">
<li id="sugg_6">
<li id="sugg_7">
<li id="sugg_8">
<li id="sugg_9">
<li id="sugg_10">
I have a search look ahead feature which I'm trying to automate. I'm trying to pick the 6th option in the list every time but I just can't seem to locate it! This is the nearest I've got but it's not working..
#Browser.div(:id, "suggestionlist").link(:index, 6).click
You should do some reading about HTML. <li> tag is not link, <a> tag is link.
So, to click <li id="sugg_6"> try this:
browser.li(:id => "sugg_6").click
To click a link inside the list item (not shown in your HTML but referenced in comments)
browser.li(:id => "sugg_6").link.click
(that presumes you want to click the first/only link inside the LI, otherwise you might need to specify an index value)
Did you try to access this element by XPath?
browser.find_elements_by_xpath("div[#id='suggestionlist'/li[6]").click

Resources