Do Edit or Delete Action in table With Dynamic ID - selenium

I encountered some problem to identify dynamic ID in table that have dynamic IDs to do some action like edit or delete, because the IDs is dynamic shown, I don't understand how to do action like edit data from code no 12345 when shown id is id=...0.
How to solving this? I'm using Selenium IDE. How we can
do edit or delete when the number id is dynamic?
Here is the HTML code
<div>
<table id="gvSomething" class="mGrid" cellspacing="0" style="border-collapse:collapse;">
<tbody>
<tr>
<th scope="col">
<a id="CODE">code no.</a>
<th scope="col">
<a id="NAME">name</a>
<th scope="col">
<a id="STATUS">active</a>
<th scope="col"> CONTROL </th>
</tr>
<tr>
<td align="center">
<span id="Code_0">12345</span>
</td>
<td>
<span id="Name_0">zero</span>
</td>
<td valign="middle" align="center">
<span id="Status_0">Yes</span>
</td>
<td valign="middle" align="center">
<input id="Edit_0" type="image" src="../Images/Edit.gif" title="Edit Record" name="Status$ctl02$imbEdit">
<input id="Delete_0" type="image" src="../Images/Delete.gif" title="Delete Record" name="Delete$ctl02$imbDelete">
</td>
</tr>
<tr>
<td align="center">
<span id="Code_1">098765</span>
</td>
<td>
<span id="Name_1">one</span>
</td>
<td valign="middle" align="center">
<span id="Status_1">Yes</span>
</td>
<td valign="middle" align="center">
<input id="Edit_1" type="image" src="../Images/Edit.gif" title="Edit Record" name="Status$ctl03$imbEdit">
<input id="Delete_1" type="image" src="../Images/Delete.gif" title="Delete Record" name="Delete$ctl03$imbDelete">
</td>
</tr>
</tbody>
</table>
Here's the sample code when data not yet added with new data
code | name | active | control
12345 (id=Code_1) zero (id=name_1) yes (id=status_1) edit(id=edit_1)/delete (id=delete_1)
098765 (id=Code_2) one (id=name_2) yes (id=status_2) edit(id=edit_2)/delete (id=delete_2)
and when there's new data added, become like this
code | name | active | control
01234 (id=Code_1) new (id=name_1) yes (id=status_1) edit(id=edit_1)/delete (id=delete_1)
12345 (id=Code_2) zero (id=name_2) yes (id=status_2) edit(id=edit_2)/delete (id=delete_2)
098765 (id=Code_3) one (id=name_3) yes (id=status_3) edit(id=edit_3)/delete (id=delete_3)
How selenium IDE know when we try to click edit image with id=edit_2 it know we want to edit data with name 12345 and name zero, not edit data with name 09876?

For things like dynamic tables, ID locators won't do the job, you're right. However Selenium has two types of locators for finding elements relative to other elements: XPath and CSS selectors. CSS selectors are usually recommended because they are faster, but since we are looking at text and not HTML tag attributes, XPath is the way to go for this problem.
Below is a locator you could use as the locator in selenium IDE if you wanted to find a row that contained the code no. 12345 and the name zero, then select the edit control in that row.
xpath=//tr[contains(., "12345") and contains(., "zero")]//input[contains(#id, "Edit")]
Here is W3school's tutorial on XPath and reference on CSS selectors

Related

How do i fetch text from <span> tag for each row

how do i fetch some of text from each row.
Here is the code:::
<table>
<tbody>
<tr>
<td style =" " >
<span calss=xxx'>Manager </span>
</td>
<td> 123</td>
<td> 456 </td>
</tr>
<tr>
<td style =" " >
<span calss=xxx'>Employee </span>
</td>
<td> 123</td>
<td> 456 </td>
</tr>
<tr>
<td style =" " >
<span calss=xxx'>Admin </span>
</td>
<td> 123</td>
<td> 456 </td>
</tr>
<tr>
<td style =" " >
<span calss=xxx'>Non-Admin </span>
</td>
<td> 123</td>
<td> 456 </td>
</tr>
</tbody>
</table>
When i am clicking on one of rows out of 21 rows, its open one popup and that contain table having 3-4 rows.
One popup may contain 4 or 3 or 2 or 1 rows and so on.
I want to fetch that text(Manager, Employee, Admin,Non-Admin) for each row, After clicking on each row.
Found all the elements,
#FindBy(xpath="//td[contains(text(),'Manager')]")
WebElement managerText; but how do i perform actions on it.
The final output should be something like as follow:
On first popup , 3 text(td) are there, should fetch and should display as follow:
Manager
Employee
Admin
On second popup , 2 texts(td) are there, should fetch and display
Employee
Admin
For third pop up, only one td(text
Non-Admin.
With the help of getText() able to fetch the value and putting in loop if one of the text is not present on popup, its failing the script. Don't know how to handle No such element exception for all 21 rows or with looping command.
If any one knows, reply. Thanks.

how to check if a tag is the last one

I am using Selenium and Java to write a test. I have the DOM below:
<tbody>
<tr>
<th>Copy</th>
<th>Subfield</th>
<th>Subfield Border</th>
<th>Field</th>
<th>Field Border</th>
</tr>
<tr id="333877">
<td>
<input type="checkbox" checked="" class="copySubfieldBorderCheck"/>
</td>
<td>a</td>
<td class="s">No</td>
<td>c</td>
<td>Yes</td>
</tr>
<tr>
<th>as</th>
<th>er</th>
<th>df</th>
<th>xc</th>
<th>xc</th>
</tr>
<tr id="333877">
<td>
<input type="checkbox" checked="" class="copySubfieldBorderCheck"/>
</td>
<td>rt</td>
<td class="noBorderBoldRed">Yes</td>
<td>ff</td>
<td>sdf</td>
</tr>
I want to get the tr that has a td tag whose text is No and alos it LAST td tag's text is Yes
I am looking for something like this:
//tr[./td[text()='No'] and ./td[text()='Yes' and isLast()]]
An easy way would be to concat the 3rd and the last cell and then filter the text on NoYes:
//tr[concat(td[3], td[last()])='NoYes']

Locate text in a page by using other text in xpath

How can I locate some text in a span in one row using other text that's in other span in other row by xpath in selenium, I don't want to traverse by html tags as html template can be changed dynamically and also I don't have a unique Id.
Below is my code with highlighted text as question and answer text :-
<div id="surveyquescontent">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<span class="mrQuestionText" style="">**Have you chosen your own date for when your electric bill is due?**</span>
</td>
</tr>
<tr>
<td>
<span style="">
<span class="mrQuestionTable" style="display:block;margin-left: 1em;">
<span id="Cell.0.0" style="">
<label for="_Q1_C0">
<span class="mrSingleText" style="">**Yes**</span>
</label>
</span>
</span>
</span>
</td>
</tr>
</tbody>
</table>
</div>
You can try to select the correct <tr> and use following-sibling::tr and continue digging to the desired <span> for example :
//tr[.//span[#class='mrQuestionText']]
/following-sibling::tr[1]
//span[#class='mrSingleText']
In above example I'm using class attribute value to get correct <tr>, you can change it easily to be using text within the <span> if you have to, or using both text and class :
//tr[.//span[
#class='mrQuestionText'
and
.='Have you chosen your own date for when your electric bill is due?'
]]
/following-sibling::tr[1]
//span[#class='mrSingleText']

click on a checkbox selenium java

Hi, I want to b able to click on a check box based on a value. Below is the html of the same:
<tbody>
<tr>
<th>Record Type</th>
<th style="text-align:center">Has Access?</th>
<th style="text-align:center">Can Create?</th>
<th style="text-align:center">Can Read All Records?</th>
</tr>
<tr>
<td>
<input id="ORMW.S2.0.Object Type" class="readOnlyField" type="text" value="Account" maxlength="255" size="25" name="ORMW.S2.0.Object Type" readonly="readonly"/>
</td>
<td align="center">
<input id="ORMW.S2.0.Has Access_VP123456" type="checkbox" onclick="cbValue(this,'ORMW.S2.0.Has\x20Access');" style="margin-left:0px;margin-top:1px;height:10px;width:10px;vertical-align:middle"/>
<input id="ORMW.S2.0.Has Access" type="hidden" value="Y" tabindex="-1" name="ORMW.S2.0.Has Access"/>
</td>
<td align="center">
<td align="center">
I want to click on checkbox based on the value account here.
Next query will search element by #value='Account' then traverse up to the first tr and then search your checkbox:
//input[#type='text' and #value='Account']/ancestor::tr[1]//input[#type='checkbox']
you can also try this:
//input[#type='checkbox' and preceding-sibling::input[#type='text' and #value='Account']]

How do I select a specific element in a table depending on the text

<table>
<tbody>
<tr>
<th style="width:200px;">Features</th>
<th style="width:300px;">Settings for this entry round</th>
<th style="width:300px;">
</tr>
<tr>
<td style="background-color: rgb(234, 234, 234);"> is_team_round </td>
<td style="text-align:center;">
<div>
<select id="account_3" onchange="changeAccount(3);" style="background-color: rgb(255, 255, 255);">
<div> </div>
</div>
</td>
<td style="text-align:center;"> Yes </td>
</tr>
<tr>
<td> is_file_uploads </td>
<td style="text-align:center;">
<div>
<select id="account_4" onchange="changeAccount(4);">
<div> </div>
</div>
</td>
<td style="text-align:center;"> Yes </td>
</tr>
</tbody>
</table>
How can I select the ID account 3 depending if the text is is_team_round?
I tried //td[contains(text(), 'is_team_round')], and it selects is_team_round.
Then I tried //td[contains(text(), 'is_team_round')]//select[#id='account_3'], but it doesn't work
Try the xpath:
//tr[./td[contains(text(), 'is_team_round')]]//select
This says to find the row (tr) that has the cell (td) with text 'is_team_round". Then find the select list inside that row.
Note: Your original solution did not work because it tried to find a select list inside the cell containing the text. As seen in the HTML, the select list is not inside the same cell. Hence, the need to use a common ancestor (ie the row).
I'd do it using 2 nested jquery's .each() function to iterate over the cells, and then testing the values with jquery's .val() or .html() function to get and compare the values.check out those functions uses and syntax at jquery.com.
hope it helped
$("#table").find("tbody td").eq('is_team_body');
id try something like this