Locate text in a page by using other text in xpath - selenium

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']

Related

ColdFusion - Using the Query Output to Store Variables For Each Row to Re-Query for Edit

I am definitely a noob at CFML and have been trying to wrap my head around this for some time. I have my query output my data with a Detail button I created that will enter into a new page to edit the information. Each #INV# would have more line items associated to them for review. My goal is to click the button and run a query based off the INV for that given row to return output of all associated line items that need some sort of approval in a new page. Then giving me the ability to edit the data.
I think I need to be able to store the INV column as a variable in my code but am not sure how to accomplish this.
Sorry if it isn't too clear!
<cfquery name="Review" datasource="FINANCE_EQMT_ACCT">
SELECT '1' as ROW, ''ABC CO.' AS OWNER, 'T1234567' AS INV, '50.00' AS TOTAL FROM TBL_TEST
</cfquery>
<div class="container col-xs-12 col-sm-12 col-md-5 col-md-push-1 col-lg-5 col-lg-push-1">
<div class="row">
<table class="table table-striped">
<legend>QUEUE</legend>
<thead>
<tr>
<th class="text-center">#</th>
<th>Owner</th>
<th>Invoice</th>
<th>Amount</th>
<th></th>
</tr>
</thead>
<tbody>
<cfoutput query="Review">
<tr>
<td class="text-center">#ROW#</td>
<td>#OWNER#</td>
<td>#INV#</td>
<td>#TOTAL#</td>
<td class="text-right"><form action="review_action.cfm" method="post"><input class="btn btn-primary" type="submit" value="Detail"></form></td>
</tr>
</cfoutput>
</tbody>
</table>
</div>
</div>
You don't need to save it. You just need to push it over to the next page
...
<td class="text-right">
<form action="review_action.cfm" method="post">
<input type="hidden" name="INV" value="#INV#" />
<input class="btn btn-primary" type="submit" value="Detail">
</form>
</td>
...
review_action.cfm will then process that value as a part of form.inv

I want to extract a text string on the webpage which is written in the form of table, but I am getting null in return

WebElement e5 = d1.findElementByXPath("/html/body/div[1]/div[3]/div/div[1]/form/div/div[1]/div/table/tbody/tr[4]/td[2]");
String a = e5.getText();
System.out.println(a);
_____________________________________________________________________________
I have used getText method. The data is stored in the table, so the strategy which I am making use of is that first of all i want to get to the location of that data using xPath funda and then I need to extract the data corresponding to that location. Can anyone suggest a better way to do so.Thanks in advance.
pls find below the html:
<div class="wrapper">
<form method="post" action="">
<input id="_csrf" type="hidden" value="psJdnI5AnxjGqT2knrZG" name="_csrf">
<div class="innercontainer">
<div class="leftpanel">
<h2>MY ACCOUNT</h2>
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<tr>
<td>
<strong>Membership ID:</strong>
</td>
<td>2800000101</td>
</tr>
<tr>
<tr>
<tr> </tr>
<tr>
</tbody>
</table>
</div>
</div>
<div class="rightpanel">
</div>
</form>
</div>
html page added above. Kindly suggest some alternative
You need to clean up your Xpath, however this should help
WebElement tableText = driver.findElement(By.xpath("yourXpath"));
String actualText = tableText.getAttribute("textContent");
System.out.println(actualText)

How to get xpath for this element

I have lot of tr tag with same structure in my page . The only way to select "Google" is by starting the XPATH from "ImgTrash.png" . Please let me know if you have idea .
<tr>
<td width="1%"/>
<td class="Fol" nowrap="" style="padding-left: 0px">
<a id="A57" href="3745474">
<img src="ImgTrash.png"/>
<span style="vertical-align:middle">Trash </span>
</a>
</td>
</tr>
<tr class="Row">
<td width="1%"/>
<td class="Fol" nowrap="" style="padding-left: 8px">
<a id="A56" href="507023">
<img src="ImgFolder.png"/>
<span style="vertical-align:middle">Google </span>
</a>
</td>
</tr>
You will need an XPath like this:
//a[img/#src = 'ImgFolder.png']/span
Breakdown:
//a searches for all <a> elements
[img/#src = 'ImgFolder.png'] only selects the <a> elements that have a child <img> with the attribute src. This attribute src should exactly match (case-sensitive) ImgFolder.png
/span at the end will get the child <span> from the previous selected <a>

Do Edit or Delete Action in table With Dynamic ID

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

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