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
Related
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.
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();
Using Xpath tool from chrome dev tools, I have been able to get xpath string and a td object. I 'm just wondering how do we find the value of text in a td object using selenium python web-drivers?
for your case you can use .gettext() method of selenium.
Once I found that, the attribute within td which contains the text is changing and i was supposed to fetch the text in those attribute.
like
<td class="table__cell" data-col-index="1">
<div class="ec-table__cell-content ">813.75</div>
</td>
<td class="table__cell" data-col-index="2">
<span class="ec-table__cell-content ">522.12</span>
</td>
So to get text within all td in a list, I used dynamic Xpath way:
By.xpath("//td[contains(#class,'table__cell')]/following::*").getText()
Try to search by xpath. Relatively to some ID.
Example of:
id("body_content")/table/tbody/tr/td[3]
in this case will be shown exact elements of 3rd column only.
I am finding it hard to create a relative xpath for a check box . I have attached the code snippet below. Firebug gives me an absolute xpath as:
html/body/div[6]/div[2]/form/table[1]/tbody/tr[3]/td[1]/input[1].
But the form changes if there is separate entry and then absolute xpath changes to:
html/body/div[6]/div[2]/form/table[1]/tbody/tr[4]/td[1]/input[1]
(or tr[5] or any number). I tried to use the relative xpath but the firebug shows the xpath in red colour (so it is invalid)-
.//*[#id='responderSelection'155939'1']
Please help.
<form id="pollGroupForm" method="post" action="/consoleapp/newSurvey.jspx">
<div class="filter"/>
<table class="dataTable">
<tbody>
<tr>
<tr class="alternateRow">
<tr>
<td>
<input id="responderSelection'155939'1" type="checkbox" value="true" name="responderSelection['155939']"/>
<input type="hidden" value="on" name="_responderSelection['155939']"/>
</td>
<td>twoway_2</td>
<td> 26161 </td>
<td>
</tr>
</tbody>
</table>
<table>
</form>
</div>
</div>
<div id="footer">
Would you try a partial search using cssSelector? I assume the numbers with the id is dynamic so the following cssSelector let's you find the checkbox with the partial match of the id(^ means starts with)
[id^='responderSelection']
If you really want xpath then contains() is the equivalence to the css above
//input[contains(#id,'responderSelection')]
There are several different ways to do this, depending on the exact conditions.
If there is only on checkbox in that table, then you can focus on that: //table//input[#type='checkbox'].
Possibly safer selector would be to look for a checkbox in your form, as that has a fixed id attribute: //form[#id='pollGroupForm']//input[#type='checkbox']. If you can, always start from fixed ids.
If you want to get the Xpath of an element which ID is changing dynamically, there is a little trick: simply inspect the element with Firebug, delete its ID by double clicking on it (delete the whole id parameter, or change the value to "")and you will get the correct Xpath.
For example if you have the following structure:
<div id="fix-id">
<div id="dynamic-id-1214151"></div>
</div>
You will get //*[#id="fix-id"]/div[1] instead of //*[#id="dynamic-id-1214151"].
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.