How to test a dropdown list in selenium which was created using the input tag (not the select tag)? - selenium

Here there are a lot of posts for testing dropdown lists which are created using the select and option tags. But even after lot of effort, I could not find even a single post for testing dropdowns created using input tag.
html code :
<input id="CEPJICNK.ProcMgmtView.InstanceAdvancedQueryHitLimitDropDownBK" autocomplete="off" value="10" readonly="true" ct="CB" lsdata="{7:'SVSDK.com.sap.dictionary.string_530_',8:'10',10:'10',12:true}" lsevents="{Select:[{'ClientAction':'submit'},{'urEventName':'COMBOBOXSELECTIONCHANGE'}],Change:[{'ClientAction':'submit'},{'urEventName':'COMBOBOXSELECTIONCHANGE'}]}" tabindex="0" ti="0" class="urEdf2TxtRadius urEdf2TxtEnbl lsEdf3TxtHlpBtn lsEdfLeftBrdRadius lsEdFieldFocus" style="width:100%;"></td><td class="lsTblEdf3HlpBtnTd"><input type="text" readonly="true" id="CEPJICNK.ProcMgmtView.InstanceAdvancedQueryHitLimitDropDownBK-btn" tabindex="-1" ti="-1" class="urBorderBox lsEdf2HlpRadius lsEdf3HlpBtn lsEdf3HlpBtnCoB lsEdf3HlpBtnFocus"></td></tr></tbody></table>
*//Current Values in dropdown are : 10,20,30,40,All*
<tbody><tr ct="ILBI" lsdata="{0:'10',4:'10'}" id="SVSDK.com.sap.dictionary.string_530_-key-0" class="urIlb2ISel"><td class="urIlb2I urColorTxtStandard">10</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'20',4:'20'}" id="SVSDK.com.sap.dictionary.string_530_-key-1" class="">
<td class="urIlb2I urColorTxtStandard">20</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'30',4:'30'}" id="SVSDK.com.sap.dictionary.string_530_-key-2" class=""><td class="urIlb2I urColorTxtStandard">30</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'40',4:'40'}" id="SVSDK.com.sap.dictionary.string_530_-key-3" class=""><td class="urIlb2I urColorTxtStandard">40</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'-1',4:'All'}" id="SVSDK.com.sap.dictionary.string_530_-key-4" class=""><td class="urIlb2I urColorTxtStandard">All</td>
<td> </td></tr></tbody></table></div>

Hi #Sumitbit2005 What approach i found is :
1. first get hold of input tag using xpath and its id
2. get lsdata attribute of this element
3. now using string operations, extract the number part
4. now that we have the number, we can access the table by giving full xpath
5. But alas the table is in a <div> tag which is in quotes. Hence the DOM can't access it directly
6. So what I do next is first access the parent element of <div> and then access its innerHTML
7. Finally we need to verify if all the expected values exist in this innerHTML
8. A clean way to do this is to treat this innerHTML as XML and
a. calculated count of values using xpath query
b. check each value using xpath query

We have the same problem. The best approach is to click on the drop down input button (-btn) and have the input box being visible. Than you can find the table in the dom and walk through it programmatically knowing exactly how and what is in it.
You can navigate the combo box by using sendKeys(Keys.ARROW_UP / DOWN) directly but you should always have the table (drop down) visible and use it for moving up and down. Having it not open and just use the input for up down will result in stale references since it appears that the input element is rewritten to the dom every 100 or so ms (no idea whats the js is causing it). Even if we refresh the element (by refinding it) the table starts to do weired stuff even causing it to have no value at all (no value attribute at the given time) or the table starts on first index again even if we were in the middle of the options.
So always have the combo box table visible.

Related

how to find xpath of the input box in the following HTML

I have a html like this
<td class="random">
<div id="randomID">Product:</div>
</td>
<td class="random">
<div id="randomID">
<input type="text" class="random">
</div>
</td>
my xpath //div[contains(text(),"Product:")] gives me the first element for which I want to send input. How do I get the input xpath so I can do input.sendkeys() on it.
You use either of the xpath to get the input tag.
Use following and index of input tag
//div[contains(text(),"Product:")]/following::input[1]
Or find the td tag and then use following-sibling
//td[.//div[contains(text(),"Product:")]]/following-sibling::td[1]//input
You can use below xpath as well.
//div[text()="Product:"]/following::input[1]
Or
//td[.//div[text()="Product:"]]/following-sibling::td[1]//input
Use following-sibling
Try with following xpath:
//td[contains(.,"Product:")]//following-sibling::td//input
For the provided code, the input is very simple to identify:
//input[#class='random']
Since I can't see any other duplicate code to force you to be more precise, I don't see the problem in using this one.
If you have multiple , and each or some of them have an input inside, in this case:
//div[text()="Product:"]/following::input[1]
Where Product is the visible text of the div that you want to use as a parent, and then you go straight in the first input taken by its index.

How to find an item in Selenium WebDriver?

I want to find the following item using Selenium. The value of the class changes whenever there is a change. This is inside a complex page (multiple iframes, and other items loaded dynamically). The only unique id is itemid, which is dynamic value and title combination. If I click on this Action, am getting another new set of complex items. I am new to Selenium. How to do that?
HTML:
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-active">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-hover">Actions <img src="../row.gif"></td>
The code I tried:
Find by Xpath
var element=driver.FindElement(By.XPath("html/body/div[id='pageContent']/iframe/#document/ht‌ml/frameset/frame[name='detailsDisplay']/#document/html/body/form[name='tableForm‌']/div[id='divToolbarContainer']/div[id='divToolbar']/div[1][class='toolbar']/tab‌​le/tbody/tr/td[title='Actions']"));
Find by Link Text
var element = driver.FindElement(By.LinkText("Actions"));
Any help would be appreciated.
Try
By.CssSelector("td[title="Actions"]");
By.CssSelector("td[itemid="xxyyy.as123"]");
By.CssSelector("td[itemid="xxyyy.as123"][title="Actions"]")
Create Dynamic CSS Selector.
For Example:
driver.FindElement(By.CssSelector("td[itemid$="xxyyy."]")).Click();
Note: In dynamic Elements, there is always a part of locator wich is fixed. we need to generate the locator using this part.
If fixed part is at starting - Use Carrot Character (^)
If fixed part is at Middle - Use Asterisk sign (*)
If fixed part is at End - Use Doller sign ($)
Finally I was able to achieve it, by using the frame names.
driver.SwitchTo().Frame("content").SwitchTo().Frame("detailsDisplay");
var element = driver.FindElement(By.XPath("//*[#id=\"divToolbar\"]/div[1]/table/tbody/tr/td[1]"));
Thanks everyone.

How to select the drop-down items which is out of tag using xpath

I've tried several xpath's for choosing the dropdown list. But nothing is worked.
Some of the xpath's I used are as follows:
By.xpath("//table/tbody/tr[2]/td[2]/span").click;
Or
By.xpath("/td[2]/span[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
Please find the below html tags, I need to select the value either 'ASYLUM SEEKER PERMIT DOCUMENT' or 'NATIONAL IDENTITY DOCUMENT'.
<tbody>
<tr id="jP2Qrg" class="z-comboitem">
<td class="z-comboitem-img"/>
<td class="z-comboitem-text">
<span class="z-comboitem-spacer"/>
ASYLUM SEEKER PERMIT DOCUMENT
</td>
</tr>
<tr id="jP2Qsg" class="z-comboitem z-comboitem-over">
<td class="z-comboitem-img"/>
<td class="z-comboitem-text">
<span class="z-comboitem-spacer"/>
NATIONAL IDENTITY DOCUMENT
</td>
</tr>
</tbody>
When I try to take xpath using firepath, it is dynamic. Every time the xpath and the id is keep changing. So please suggest the xpath which works.
The text is not inside the span text and it is in second td tag. you can try with small change in your code as given below.
By.xpath("//table/tbody/tr[2]/td[2]").click;
or
By.xpath("//td[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
Can you check this..,
By.xpath("//*[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
By.xpath("//*[contains(text(),'ASYLUM SEEKER PERMIT DOCUMENT')]").click;
Try to use below XPath to match required option:
By.xpath("//td[normalize-space()='ASYLUM SEEKER PERMIT DOCUMENT']").click;
or
By.xpath("//td[.='ASYLUM SEEKER PERMIT DOCUMENT']").click;
As #Murthi said, text node is not a child of span, but td. Note that there are some specifics in locating text nodes
Here is the Answer to your Question:
To click on ASYLUM SEEKER PERMIT DOCUMENT you can use the following xpath:
By.xpath("//td[contains(.,'ASYLUM SEEKER PERMIT DOCUMENT') and not (#class='z-comboitem-spacer')]").click;
Let me know if this Answers your Question.
How about this xpaths,
Updated code
(//td[2])[1]//*[starts-with(#class,"z-com")]; // Selects ASYLUM..
(//td[2])[2]//*[starts-with(#class,"z-com")]; // Selects NATIONAL..
This should work if it doesn't have same class name in the same position.
Old xpath's
driver.findelement(By.xpath("(//td[2])[1]")); // selects ASYLUM SEEKER PERMIT DOCUMENT
driver.findelement(By.xpath("(//td[2])[2]")); // selects NATIONAL IDENTITY DOCUMENT
Find out the solution for selecting the dropdown options.
By using List method, I took the list of options available in that field and selected by using clicking the index value of list.
List<WebElement> Idtype = driver.findElements(By.xpath("//tr[2]/td[2][#class = 'z-comboitem-text']"));
Idtype.get(0).click();

Handling of dynamic ids through selenium webdriver

Automate an application through selenium where id changes dynamically.how can i handle this.Pls help me..
HTML code is:-
<table border="0" cellpadding="0" cellspacing="0" width="1000px">
<tbody><tr id="ctl00_ctl00_MainContent_CarQuoteMainContent_rpQuotes_trSelectedQuote_0">
<td align="center" valign="middle" width="12%">
<input id="ctl00_ctl00_MainContent_CarQuoteMainContent_rpQuotes_chkCompare_0" name="ctl00$ctl00$MainContent$CarQuoteMainContent$rpQuotes$ctl00$chkCompare" type="checkbox">
</td>
In both the cases ( and ), I assume that the first part of the ID is unique. So you can use something like this.
//tr[contains(#id,'ctl00_')] and for input field //input[contains(#id,'ctl00_')].
Those don't look like dynamic IDs, but rather non-content-specific row IDs for elements in a list.
If that's the case, you can't immediately ascertain 'This row element is displaying data for MyCarQuotes.com' from this information alone as there's nothing in the HTML shown to base that query on.
If there's something in the rows you can use to 'identify' the content (eg a company name) - and you have a specific 'thing' you want to interact with - you could encapsulate the lookup and do something like
CheckboxForQuoteFromCompany("MyCarQuotes.com").Click();
If you're able to post more of the HTML (at least a full row), and more importantly the intention of your test, we may be able to be of more help.

Checkbox and dropdownbox in selenium

Below is my code
Below is a tag.
<TR id="oldcontent" bgcolor="#D0D0D0">
<TD id="ignore" style="vertical-align:middle">
<input type="checkbox" name="selectedId" value="22047"onclick="updateSelectionList('<%=campaign.getId()%>')">
</TD>
<TD id="oldcontent">Code</TD>
<TD ALIGN="left" id="oldcontent">
<select name="status" style="width=150" id="newcontentformat">
<option value="14" selected="selected">text1</option>
<option value="15">text2</option>
</TD>
<TR>
Here
1)i need to click the checkbox which has dynamically generated value without any string.
2)Can i select the checkbox based on the text "Code" present in next after checkbox?
3)I need to pick up text2 in the dropdown with name status
4)Lastly the issue is this can appear any where in web page,everytime i run the test case.So i need to check the checkbox using String "code",2ndly i need to select value from dropdown which has name staus.There are other dropdown boxes with same name status.So how do i specifically do this?
To get the select list in the row that has the text "Code", you can use the xpath:
//tr[./td[text()='Code']]/td/select
Similarly, for the checkbox, you can use the xpath:
//tr[./td[text()='Code']]/td/input[#type='checkbox']
I believe then the selenium code you want is:
selenium.select("//tr[./td[text()='Code']]/td/select", "text2")
selenium.check("//tr[./td[text()='Code']]/td/input[#type='checkbox']")
As long as there are names available in the HTML, you can use Name to locate an element.
For click on check box you can write:
selenium.click(//input[#name='selectedId']);
To go to check box using text locator should be like:
//div[text()='text you want to precede']/preceding::input[#name='selectedId'];
(here you can use any property instead of name.)
To pickup text from dropdowm:
selenium.select(//select[#name='status'],"text2");
If you use such locators (which are position independent), no matter where your element appears on the screen, you can locate with the help of these locators....
To know more about locating preceding or following Download Ebook:Selenium.1.0.Testing.Tools.Beginners.Guide