Selecting an element from a drop-down with Protractor - java

I am having serious trouble using the regular selenium webdriver on one page of my application which is build with Angular. I have added the dependency of ngwebdriver in my pom.xml and so far I have solved some of the issues. Only with the drop-down, I still haven't figured out how to do this. This is the drop-down I am talking about:
<d3-data-list _nghost-c8="" ng-reflect-data-source="[object Object]" ng-
reflect-html-id="documents_glossary_entityPicke" ng-reflect-allow-blank-
option="true" ng-reflect-blank-option-text="All entities" ng-reflect-default-
caption="Choose an entity"><div _ngcontent-c8="" class="dropdown d3-data-
list" id="documents_glossary_entityPicker">
<button _ngcontent-c8="" aria-expanded="true" aria-haspopup="true" class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button" id="documents_glossary_entityPicker_button">
All entities
<span _ngcontent-c8="" class="caret"
id="documents_glossary_entityPicker_caret"></span>
</button>
<ul _ngcontent-c8="" aria-labelledby="dropdownMenu1" class="dropdown-menu"
ng-reflect-infinite-scroll-handle-in-progress="false"
id="documents_glossary_entityPicker_dropdown">
<!--bindings={
"ng-reflect-ng-if": "true"
}--><li _ngcontent-c8="">
<a _ngcontent-c8=""
id="documents_glossary_entityPicker_blank_option">All entities</a>
</li>
then the options from the drop-down are like this:
</li><li _ngcontent-c8="">
<a _ngcontent-c8="" id="documents_glossary_entityPicker_option_2">Entity three</a>
</li>
The problem is that the order of the elements in my drop-down is not maintained because if an element (entity) is edited on another page then it will be at the bottom of the list. So I can never really know where exactly my element is in the drop-down list.
Is there something like browser.findElement(ByAngular.buttonText("text")) which I can use in my case? Thanks.

I guess you're looking for the filter() or .reduce() possibilities.
For .reduce() check the example here
For .filter() your code should look something like this:
element.all(by.css('ul li a')).filter(function (elm) {
return elm.getAttribute('value').then(function (text) {
return text === "TheValueYouLookFor";
});
}).first().click();
I didn't test this .filter myself, but it's pretty straight forward from the Protractor documentation.

Related

extracting attribute value of parent element using Selenium

Experienced with Java, pretty new to Selenium, locators, etc.
Buried deep in some HTML is several similar divisions:
<div tabgroup="topTabs__County Summary" sectiongroup class="field TextDescription tab">
<label for="request_48543">
<span class="label">Monument</span>
</label>
</div>
<div tabgroup="topTabs__County Summary" sectiongroup class="field DropDownList readonly tab">
<label for="request_48543">
<span class="label">Geolocation</span>
</label>
</div>
<div tabgroup="topTabs__County Summary" sectiongroup class="field SingleLineText tab">
<label for="request_48543">
<span class="label">Intersection</span>
</label>
</div
I need some Selenium magic to find a label with a specific value then backtrack to find that label's division and from that division extract the value of a given attribute. Drilling down seems fairly easy but how does one "back up" ?
For example, given "Geolocation" I'd like to extract "field DropDownList readonly tab"
I've tried things like
WebElement chill = m.findElement(By.xpath("../..//span[text='Geolocation']"));
to no avail
You reversed the order of going to the parent element, and you need () in text. The xpath should be
"//span[text()='Geolocation']/../.."
Another option is to look for an element that has a chilled with "Geolocation" text
"//div[.//span[text()='Geolocation']]"
this might give you more results, depends on the html structure that is not in the question. In that case you can add unique attribute, for example tabgroup
"//div[.//span[text()='Geolocation']][#tabgroup]"
this will return only <div> tag that has tabgroup attribute.
To extract the data use getAttribute("class") on chill WebElement

How to enter information in a combo box with look up using Selenium webdriver?

I am having trouble with automating data entry in a combo-box with lookup/auto filling option element. The automation task is filtering data based on a company's name.
driver.findElement(By.xpath(".//*[#id='s2id_company_id']")).click();
driver.findElement(By.xpath(".//*[#id='s2id_company_id']")).sendKeys(companyname);
((WebElement) driver).sendKeys(Keys.ENTER);
The script runs perfectly up until entering the data.
Relevant HTML:
<div id="div_ff_company_id" class="ff_item filter-field" data-type="filter-field" condition="company_id" operator="is_in" container="multi_select" type="default" data-label="Customer">
<label class="control-label">Customer</label>
<div class="select2-container select2-container-multi input-xlarge filter_item" id="s2id_company_id" style="width:100%"><ul class="select2-choices"> <li class="select2-search-field"> <label for="s2id_autogen79" class="select2-offscreen"></label> <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen79" placeholder="" style="width: 10px;"> </li></ul></div><input type="hidden" id="company_id" style="width:100%" class="input-xlarge filter_item select2-offscreen" tabindex="-1" value="">
Can you guys tell me if there is something I am overlooking?
After clicking on it ... I mean after
driver.findElement(By.xpath(".//*[#id='s2id_company_id']")).click(); statement then use
WebElement currentElement = driver.switchTo().activeElement()// which give the active or currently focused element.
Try #1 : currentElement.sendKeys(companyname);
Try #2 : inspect the element in Dom then use sendkeys
Try #3 : use executeScript() which takes function calls and raw JS, too. You can return a value from it and you can pass lots of complicated arguments to it.
For reference http://www.seleniumhq.org/docs/03_webdriver.jsp#using-javascript
Hope it helps

Clicking span button by id of the parent(with id) - selenium with java

Is it possible to click a span button which is under the div element, also I would like to use the id of the input to make my test stable.
HTML:
<div class="idit-go">
<input id="IDITForm#checkRuleVO" class="GoLong align-left idit-go-text" type="text" value="" title="Validation Rule" tabindex="257" name="unknown" ignoredisableenableaction="true" helptextinfo="" disabled="">
<div class="idit-go-buttons">
<span id="IDITForm#checkRuleVOGoClearButtonClear" class="idit-go-clear" title="Clear" tabindex="259" onclick="clearField('IDITForm#checkRuleVOhidden');clearField('IDITForm#checkRuleVO');;">
<span class="fa-stack idit-go-button" value="" title="" tabindex="258" onclick="newPerformSubAction('selectRule',true);">
<i class="fa fa-stack-1x fa-square"></i>
<i class="fa fa-stack-1x fa-ellipsis-h"></i>
</span>
</div>
How it looks on screen:
As you can see the 3 dots button to the right, that is the button I'd like to click. Thanks in advance.
In this case you could identify the span that has the id attribute and then just use following-sibling (from your example they seems to be on the same level).
driver.findElement(By.xpath(".//[#id='IDITForm#checkRuleVOGoClearButtonClear']/following-sibling::span"));
using css - "span[id='IDITForm#checkRuleVOGoClearButtonClear'] + span[class='fa-stack idit-go-button']"
Okay, no-one got this from what I can see, so I can have a go as well! :)
Here we go Zvika :
element = driver.findElement(By.xpath("//div[#class='idit-go']//span[#class='fa-stack idit-go-button']"));
element.click();
(locating and clicking an element with span id="IDITForm#checkRuleVOGoClearButtonClear")
Best of luck man!:)

I can't find the Web Element oh the page

I'm trying to find the web element "Grupo de ParĂ¢metros" but when I use selenium IDE to locate the xpath:idRelative //div[#id='menu']/ul/li[6]/ul/li[4]/ul/li/ul/li[2]/a/span or xpath:postion //li[6]/ul/li[4]/ul/li/ul/li[2]/a/span does not occur anything . I'm using eclipse/java.
This is the code of page
<li class="ui-menuitem ui-widget ui-corner-all ui-menuitem-active" role="menuitem">
<a class="ui-menuitem-link ui-corner-all ui-state-hover" onclick="PrimeFaces.ab({source:'menu',global:false,params: [{name:'menu_menuid',value:'5_3_0_1'}],formId:'j_idt8'});return false;" href="#" title="" tabindex="-1">
<span class="ui-menuitem-text">**Grupo de ParĂ¢metros**</span>
</a>
</li>
And this is my automatized test
driver.get(...);
driver.findElement(By.xpath("//div[#id='menu']/ul/li[6]/ul/li[4]/ul/li/ul/li[2]/a/span"));
driver.findElement(By.id("paramGroupAlias")).clear();
driver.findElement(By.id("paramGroupAlias")).sendKeys("box");
driver.findElement(By.id("pesquisar")).click();
WebElement retorno = driver.findElement(By.id("j_idt64_data"));
System.out.println(" Retorno "+ retorno.getText() );
I also tried this but I also couldn't make it from Selenium IDE when I exported it so I programmed it in my Java proyect with JQuery. I think it will be good for you something like this:
#ui-menuitem ui-widget ui-corner-all ui-menuitem-active > a > span
Where you reference the class of the first element of your page and you descend "levels" in the page.
But I also made reference to the object that I wanted to refer to like this:
#ui-menuitem-text
I used it in my Java project and it worked but I tried this with 3 'divs' one inside the others. Anyway, I think it will work to you.
Please let me know if it works.

How to click a link from list selenium2

I'm using Selenium 2 and I want to click an 'invite' link for Name3. How can I do that?
here is the html code:
<ul>
<li>
<label for="511565484">
<img src="pic1">Name1</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
<li>
<label for="535963597">
<img src="pic2">Name2</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
<li>
<label for="561708219">
<img src="pic3">Name3</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
</ul>
Seems likely it can only be done with XPath:
//label[text()='Name3']/following-sibling::a
element2 = driver.findElement(By.xpath("//img[()text='Name3']/a"));
element2.click();
If XPATH isn't the most usable thing for you, you can always do something like this (Ruby implementation of Webdriver... but it's all the same):
invite_links = driver.find_elements(:class_name, "sendInvite")
invite_links now contains an array of all matches, so your next step is pretty easy:
invite_links[2].click()
Or the way I'd do it:
driver.find_elements(:class_name, "sendInvite")[2].click
This is a little easier for me to read than XPATH, because I don't use it that often.

Categories