identifying xpath from Ext JS application - xpath

I'm trying to automated a application designed in Ext Js, xpath identification is become complex. Please help in getting the xpath for the below scenario.
For a dropdown having a set of names, need to select a name from the list available.
I'm using a mouse action to locate the drop down name and then moveToElement particular element in the List of menu present.
For example If the menu is containing a list of names like, Abi, Ashwini Asha, Ashwini, Diva.
Using parameterizing I'm able to select the names from the menu.
But when I need to select Ashwini from the menu ,since Ashwini Asha is already present in the menu , Ashwini Asha object gets clicked.
contains() is not working here. Which function should I use?
The below is the code which am using :
html :
<div id="combo-1023-inputWrap" class="x-form-text-wrap x-form- text-wrap-default" role="presentation" data-ref="inputWrap">
<input id="combo-1023-inputEl" class="x-form-field x-form-text x-form-text-default " type="text" autocomplete="off" aria-autocomplete="list" aria-expanded="false" aria-haspopup="true" aria-required="false" aria-invalid="false" aria-readonly="false" aria-disabled="false" aria-hidden="false" role="combobox" value="Triton" name="selectedName" size="1" data-ref="inputEl" data-componentid="combo-1023">
......
<li>------------ Ashwini Asha</li>
<li>------------ Ashwini</li>
</div>
to click on the menu :
action.moveToElement(driver.findElement(By.xpath("//input[#name='selectedName']"))).click().perform();
to load the data in the menu :
wait.until(ExpectedConditions.visibiltyOfElementLocated(By.xpath("//li[contains(text(),'"+NameParameter+"')]")));
click the object :
action.moveToElement(driver.findElement(By.xpath("//li[contains(text(),'"+NameParameter+"')]"))).click().perform();
Let me know how to select the name Ashwini alone?
Thanks

No need to use contains in your xpath. Go for exact match of the name.
By.xpath("//li[text()='"+NameParameter+"']")
or
By.xpath("//li[.='"+NameParameter+"']")

If I'm understanding this correctly, you could use:
Driver.Instance.findElements(...
This would return an array in which you could just iterate and perform actions on based on the .text of the element.

Related

Not able to click a dropdown field which is designed in the DOJO Html using selenium

I am not able to click an arrow drop down filed in my application using selenium web driver.
I tried lot of XPath using class name and relative XPath
This is the code used for the problem
<span class="dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport"
data-dojo-attach-point="iconNode"></span>
Please add some more information from your HTML i just add some text and xpaths are like that
<span class="dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport"
data-dojo-attach-point="iconNode">test</span>
Xpaths are:
//span[#class='dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport']
or
//span[#data-dojo-attach-point='iconNode']
or
//span[#data-dojo-attach-point='iconNode' and #class='dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport']
Add some more informaton if you have any concern
dojo combo are basically <input type= "text">, once u click on it or type the first letter of the option you want to select, a <div> is attached to the html body which has following structure:-
<div resultname="option name" resultvalue="option value" class="dojoComboBoxItem dojoComboBoxItemEven ">Option Value</div>
now there are 3 steps to select from dojo
identify the input text
type the first few letters of the option you want to select
create dynamic xpath to select the option
the code goes as followes
String optionName = "Option You Want to Select";
WebElement dojoBox = driver.findElement(By.xpath("<provide the xpath here>"));
dojoBox.sendKeys(optionName.substring(0,2));
driver.findElement(By.xpath("//*[#id='page-home']/span/div[#resultvalue='" + optionName + "']")).click();
if you are not sure about the dynamic xpath structure, then manually select the option, and inspect the div you have added, generally it should have the similar structure.

not able to click radio button element by xpath in selenium using python

Below is my HTML
<div id="slectrole" class="collapse in" role="tabpanel" aria-labelledby="selectrole">
<div class="panel-body">
<div class="dropdown">
<input class="search-control jsSayt jsRolesFreeText" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Eg: Delivery, BPO, Driver'" placeholder="Eg: Delivery, BPO, Driver" value="" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown" type="text">
<ul class="jsSaytList jsRolesFilter">
<li id="jsFilter_subRole_1" class="checkbox-inline jsFilterSubRole jsRoleValue_1" data-value="Accountant">
<input id="Accountant" class="radio-custom jsFilterRadio jsRole" value="Accountant" name="Role" data-roleid="1" type="radio">
<label class="radio-custom-label" for="Accountant">Accountant</label>
Below is the code I am using to click the radio button:
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#id='slectrole']/descendant::li[#data-value='Accountant']/label[#for='Accountant']")))
driver.find_element_by_xpath("//div[#id='slectrole']/descendant::li[#data-value='Accountant']/label[#for='Accountant']").click()
The code runs ok but it does not select the radio button.
OK, so I can understand your frustration, I tried your code and wasn't able to .click() (select) the element when located via xpath. See bellow print-screen:
As you can see, it was only clicking the radio-button when issuing a .click() via a CSS-located element.
Question No.1: Are you bound to the xpath locator strategy in one way or another?
If NOT, then just use a regulat CSS selector: 'input[id="Accountant"]'.
Else, you have to figure out what is wrong with the website you are testing, or switch to another WebElement locator strategy. (e.g.: ID, Class, CSS, LinkText, etc.)
If you would opt to go with the CSS locator-strategy, then your code would look like this:
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']")))
driver.find_element_by_css("input[id='Accountant']").click()
Alternatively, you can try to click on the <label> tag attached to the radio-button, which in my console works the same way:
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']")))
driver.find_element_by_css("label[for='Accountant']").click()
Explanation: In a real-life scenario, you can select the radio-button both via the actual radio-button, or via its label. That's why your solution worked.
Question No.2: Why are you using such a long xpath selector?
In order to have a optimal selector, you should ALWAYS go with the shortest, combination of tags/attributes that will UNIQUELY identify your target element. Else you will be susceptible to website changes, flaky test cases, etc.
You can perform the click on the drop down and then wait for the radio button to appear, before clicking it. Hence, try following:
driver.find_element_by_xpath("//div[#id='slectrole']/div/div[#class='dropdown']/input[1]")).click()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[#id='slectrole']/descendant::li[#data-value='Accountant']/input[1]')))
driver.find_element_by_xpath("//div[#id='slectrole']/descendant::li[#data-value='Accountant']/input[1]").click()
Let me know, if above code works for you.

Click a button with XPath containing partial id and title in Selenium IDE

Using Selenium IDE, I'm trying to click a button within a table on a webpage using XPath with a partial id and a title from the element. The XPath I'm using is:
xpath=//*[contains(#id, 'ctl00_btnAircraftMapCell')]//*[contains(#title, 'Select Seat')]
and thats the entire html code for an example of the buttons im trying to click:
<li id="ctl00_MainContent_repAircraftMap_ctl20_repAircraftMapRow‌​_ctl00_liAircraftMap‌​Cell" class="">
<a id="ctl00_MainContent_repAircraftMap_ctl20_repAircraftMapRow‌​_ctl00_btnAircraftMa‌​pCell" href="javascript:void(0)" seatnumber="20A" mapbindattribute="1124" title="Select Seat 20A" onclick="SeatClick(1124);"></a>
</li>
Am I constructing this incorrectly? It's not working!
Now that you have provided your HTML sample, we're able to see that your XPath is slightly wrong. While it's valid XPath, it's logically wrong.
You've got:
//*[contains(#id, 'ctl00_btnAircraftMapCell')]//*[contains(#title, 'Select Seat')]
Which translates into:
Get me all the elements that have an ID that contains ctl00_btnAircraftMapCell. Out of these elements, get any child elements that have a title that contains Select Seat.
What you actually want is:
//a[contains(#id, 'ctl00_btnAircraftMapCell') and contains(#title, 'Select Seat')]
Which translates into:
Get me all the anchor elements that have both: an id that contains ctl00_btnAircraftMapCell and a title that contains Select Seat.

Extracting info from button in Ruby

I have a line of code that links to a button on a HTML page and I'm wondering is there anyway that I can extract a specific value?
<input type="button" style="cursor: pointer;" onclick="selector(item,012345)"<br />
Is there anyway that I can extract the number 012345 from the code above?
My current attempt is:
temp = doc.xpath(//div....../input)
but that returns the raw data of the whole button.
I can't tell how to select the right input without more context but in general you would do:
doc.at('input')[:onclick][/\d+/]

Autocomplete DropDown Menu Testing using WatiN

I am using WatiN to test an autocomplete drop down.
When a user types in the field after 3 characters have been entered jquery autocomplete is triggered and an un-ordered list is shown. It is mandatory for the user to select from the list.
I am unable to make a selection/trigger the autocomplete from the list using WatiN.
Here is some of the html the developers have used:
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; display: block; width: 275px; top: 301px; left: 262px; ">
<li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">ABC DEFGHIJ </a></li>
<li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">ABC KLMNOPQ </a></li>
</ul>
They are using the jQuery UI autocomplete widget: http://jqueryui.com/demos/autocomplete/
Googling for jQuery UI autocomplete testing, I found this Stack Overflow Q&A:
Testing JQuery autocomplete ui with cucumber
containing what seemed to be the crucial information: “You need to first trigger a mouseover, then a click”
Then I Googled for WatiN mouseover, and found http://blogs.telerik.com/testing/posts/08-05-29/how_to_select_radcombobox_item_with_watin.aspx
This has a little code sample that includes:
Div divStudent3 = ie.Div(Find.ById("idRadComboBox_c2"));
divStudent3.FireEvent("onmouseover");
divStudent3.Click();
(to be clear our development code does not use telerik controls this is just an example)
At this point I thought I had a plan for how to drive this:
Type part of the desired value into the field (e.g. “ABC”)
Find a <ul> element with class “ui-autocomplete” and display style “block”, waiting until it is present
Within that <ul> element, find the <li> element whose text is the desired value (e.g. “ABC DEFGHIJ”)
Fire the “onmouseover” event on that <li> element
Click the <li> element.
I found two problems: firstly, that WatiN’s typing into the input field was very bad at triggering the appearance of the autocomplete menu,
and secondly that clicking on the menu item isn’t causing the autocomplete to occur.
I found that sending a downarrow key event to the input field encouraged the menu to appear, but didn’t cause the top menu item to highlight
(whereas if you type in manually and hit down arrow it does). Getting the menu item properly activated
(including getting its ID set to ui-active-menuitem) may be the missing link here.
Can anyone help me to understand and solve the two problems I have mentioned?
It took a bit, but here is a working example.
Key points
Call the JQuery object search method. This gets the dropdown list
to show.
then fire onmouseover the item you want.
Then click the item you want.
To get it to select the item correctly, I've needed to do all three above in that specific order.
Code
string searchValue = "c";
string selectItem = "COBOL";
ie.GoTo("http://jqueryui.com/demos/autocomplete/default.html");
ie.TextField("tags").TypeText(searchValue);
ie.Eval(#"$('#tags').autocomplete('search')");
ie.List(Find.ByClass("ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all")).ListItem(Find.ByText(selectItem)).Links[0].FireEvent("onmouseover");
ie.List(Find.ByClass("ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all")).ListItem(Find.ByText(selectItem)).Links[0].Click();
The above works using Watin 2.1. It won't work on WatiN 2.0 RC. I didn't check the actual 2.0 release. 2.0 RC doesn't have the List and ListItem objects. Tested only on IE8.
I have also run into a similar problem in an application that I am testing. When I type in the textfield using TypeText, the characters get typed twice.
What we did is as follows.
string mySubStr = value.Substring(0, value.Length - 3);
datavalue.Value = mySubStr;
datavalue.AppendText(value.Substring(value.Length - 3, 3));
Thread.Sleep(500);
datavalue.KeyDown((char)System.Windows.Forms.Keys.Down);
datavalue.KeyDown((char)System.Windows.Forms.Keys.Enter);
where datavalue is a reference to the textfield and value is the value that is to be keyed in.

Resources