XPath to select specific text inside of text block - xpath

I am trying to figure out a way to pull specific values out of a big long text block.
So far I have //td[#class="PadLeft10"] which returns me a big long value starting with the company name and ending with the "View More Info" piece.
I am trying to break my results up into segments, so for example I want my code to look for the words "Primary Contact:" and then return the text that follows that, ending at the <br/>.
I need to get the Company Name, which is always the first bit of text, then the Primary Contact, then the Address, then the Phone and Fax, then the Website, and the Organization type.
The problem is that not every record has all the values. As you can see, the second entry has the address and website, but the first one doesn't.
I am using the Dataminer Chrome Plugin, for anyone familiar with that. It has separate xpath for rows and columns, so I am going to try to make a bunch of different columns that correspond to each of the fields that I am looking for.
Any direction would be greatly appreciated.
<td align="left" valign="top" width="2%">
<script>
if (0 == 1) document.write('<img src="https://website.com" border="0" alt=""/>');
</script>
<br/><br/></td>
<td class="PadLeft10" align="left" valign="top" width="32%" style="padding-left: 15px;">
<span style="font-weight: bold;font-size: 12pt;"><br/>Company Name Here</span><br/>Primary Contact: Mr. Eric Cartman <br/>Phone: (555) 555-5555<br/>Fax: (333) 333-3333<span style="text-decoration: underline;color: #0000ff"></span><br/>Organization Type: Distributor Branch
<br/>
» View More Info<br/>
<br/>
</td>
<td align="left" valign="top" width="2%">
<script>
if (0 == 1) document.write('<img src="https://website.com" border="0" alt=""/>');
</script>
<br/><br/></td>
<td class="PadLeft10" align="left" valign="top" width="32%" style="padding-left: 15px;">
<span style="font-weight: bold;font-size: 12pt;"><br/>Other Company</span><br/>Primary Contact: Mr. Jimmy Valmer<br/>100 N Ohio St 2rd Fl<br/>Rochester, IN 54225<br/>United States<br/>Phone: (888) 888-8888<br/>Fax: (999) 999-9999<span style="text-decoration: underline;color: #0000ff"><br/>Web Site: http://www.companywebsite.com</span><br/>Organization Type: Financial Service
<br/>
» View More Info<br/>
<br/>
</td>
</tr>
<tr>

I am new to xpath, but the least i can say: if you are the creator of the html code, you absolutely need to change it to be more structured
like : Primary Contact:<span id/class='primaryContact'>..</span>
Or else, you can get the elements by this selector (to edit) //td[#class="PadLeft10"]//child::span//following-sibling::text()[1] split by ':' and then proceed, but this solution stay just a diy.

Any direction would be greatly appreciated.
As far as a direction, the sections within table cell that you mention are neither nested DOM items, nor sibling-type DOM nodes. Those are sequential html elements that require special processing.
<br/>Company Name Here</span>
<br/>Primary Contact: Mr. Eric Cartman
<br/>Phone: (555) 555-5555
<br/>...
Both xpath and regex can be leveraged for such a case.

You can select the text node you're looking for using a predicate and the contains function:
//td[#class="PadLeft10"]/text()[contains(., "Primary Contact:")]
Then you can get the substring using the substring-after function:
substring-after(
//td[#class="PadLeft10"]/text()[contains(., "Primary Contact:")],
'Primary Contact:'
)
And remove leading and trailing whitespace using normalize-space:
normalize-space(
substring-after(
//td[#class="PadLeft10"]/text()[contains(., "Primary Contact:")],
'Primary Contact:'
)
)

Related

How to write XPATH in nested elements of siblings

I have such DOM structure:
<td>
<div class="name">Max</div>
</td>
<td>Sales Officer</td>
<td>mail#mail.com</td>
<td class="links">
<a class="edit" href="/edit"><i class="second"></i>Edit Profile</a>
</td>
The main goal is to click on Edit button for particular user. And I have different users (they are in another rows of table).
I need to get xpath which contains the combination of name Max and text Edit Profile.
The main problem for me that both elements are in sibling td tags.
I've never written anything similar before.
//a[text()='Max'] //i[text()='Edit Profile']
What should I add between this to xpathes?
This one should do the trick:
//td[a="Max"]/following-sibling::td/a[.="Edit Profile"]

Find an element by text, get xpath and put in a list as every td is a unique element - selenium webdriver junit

I have a dynamically generated table with thead and tbody. Here is the example:
<tbody id="tableId" class="someclass1">
<tr id="rowId1" class="somesubclass">
<td id="item1" class="othersubclass">
<span class="attr">john Doe</span>
<td id="item2">
<span class="attr">55</span>
<td id="item3">
<span class="attr">5 street</span>
<td id="item2">
<span class="attr">cat</span>
<tr id="rowId2" class="somesubclass2">
<td id="item1" class="othersubclass2">
<span class="attr">joe smith</span>
<td id="item2">
<span class="attr">60</span>
<td id="item3">
<span class="attr">2 street</span>
<td id="item2">
<span class="attr">dog</span>
|joe doe|55|5 street|cat|
|joe foo|60|1 street|dog|
I would like to locate a row by text where the name is joe doe and put it into a list.
Locatior is working with this, but it is not a list.
#FindBy(xpath = "//tbody[contains(#id,'tableId')]/tr[td//text()[contains(.,'joe doe')]]")
private List<WebElement> list;
I got the list, but I cannot iterate over because everything has put into the first position like this:
list first element is: joe doe 55 5 street cat so this is the [0] element
How can I locate this row as a list where every td is a unique element in the list.
(iterate over, converting to string etc. is not a problem, just the locator.)
Thanks!
I don't understand what's for the -1. Anyway, here is the answer for others, hope this helps!
There is a good plugin for google kinda firepath: SelAssist, I use it to test my xpath locators.
The problem was that, td was missing. So the proper locator is
//tbody[contains(#id,'tableId')]/tr[td//text()[contains(.,'joe doe')]]/td
and it gives me list of elements, not only one.
Cheers!

Selecting checkbox from the line that contains word Capybara

I have been searching answer for week.
I need to create automatic tests and one of them has to delete a line from table. This line looks like this:
Html looks like this:
<tr>
<td colspan="1" class="name" rowspan="1">
Capybara852
</td>
<td colspan="1" class="description" rowspan="1">This whitelist is added by Capybara automated test</td>
<td colspan="1" class="whitelistType" rowspan="1">Internal</td>
<td colspan="1" class="status" rowspan="1">None</td>
<td colspan="1" class="active" rowspan="1">false</td>
<td colspan="1" class="msCount" rowspan="1">40</td>
<td colspan="1" class="modifiedBy" rowspan="1">admin</td>
<td colspan="1" class="modifiedOn" rowspan="1">26.06.2014 11:08</td>
<td colspan="1" class="selected" rowspan="1">
<input class="check" onclick="disableButton('delete', false);" id="check_0" name="check_0" type="checkbox">
<img id="check_0_icon" class="t-error-icon" alt="" src="/mpromoter/assets/4716a6a0a357181/core/spacer.gif" style="display: none;">
</td>
</tr>
I need to check this checkbox that stands in the same line where is written Capybara. I can't select this checkbox because its id may be different every time I run these automatic tests.
I am asking, how can I select checkbox without its own id or name or class, I need to select checkbox that stands in the same line that contains Capybara
It is a href part there.
I can select checkbox with its id. But I want to select the checkbox with unknown id from the line that contains the word capybara :)
I have tried many different things but nothing works...
So I am asking for some help. What I have to do, any suggestions?
I am not sure if I understand what are you asking but...
So you can take each row from your table and then check if the row has the value 'Capybara'
$('#stack tbody tr').find('td a').text().match(new RegExp('Capybara'))
If that is true, from the same row do what you want with the checkbox.
I need to do to many things to give you a capybara version, but you just need to translate that to capybara.
Hope that helps.
I would suggest the approach you use be:
Determine how you can find the row you want (ie contains "Capybara")
Use the within method to find the checkbox within that row
For example, you can find any row that contains the word "Capybara" anywhere using:
find('tr', :text => 'Capybara')
You can use these same find options in within. When finding the checkbox within the within block, Capybara will only search in that row. So if you do not care where the word "Capybara" shows up in the row, you can do:
within('tr', :text => 'Capybara') do
find('input.check').set(true)
end
If needed, you can change the within options to be more specific. For example, you might only want rows where the first column, which has class "name", has the word "Capybara" (rather than "Capybara" just being in the description column). This could be done with:
within(:xpath, '//tr[td[#class="name" and contains(., "Capybara")]]') do
find('input.check').set(true)
end

Trouble selecting checkbox on website

after a couple of days of trying, and failing, i need some help please.
I'm trying to select a check box on an website, but it doesn't seems to work as I want to.
the check box is part of a row in a table, together with some other data. The idea is that I log in on the site (http://www.apf.asn.au), go to the table, find the numbers I need from my excel sheet, and select the box that is in the row the number is found.
the log in and navigate to table part is working ok, but I can't find the proper way to select the box.
code from the site:
<div class="table">
<div>
<table class="table APFGrid StudentsSearch"
id="ctl00_ContentPlaceHolderMainNoAjax_gvJumpRecordsList"
style="border-collapse: collapse;" border="1" rules="all" cellspacing="0"
<tbody>
<tr>...</tr> 'Header
<tr>
<td>
<span class="clsSelect">
<input
name="ctl00$ContentPlaceHolderMainNoAjax$gvJumpRecordsList$ctl02$chkSelect"
id="ctl00_ContentPlaceHolderMainNoAjax_gvJumpRecordsList_ctl02_chkSelect"
onclick="javascript:ToggleCheckBoxSelection;" type="checkbox"></input>
</span>
</td> 'Box need to check
<td class="Name">...</td>
<td class="Name">...</td>
<td class="DateBirth">...</td>
<td class="APFNum">...</td> 'Number I'm looking for
<td class="JumpType">...</td>
<td class="DateBooked">...</td>
<td>...</td>
<td class="Name">...</td>
</tr>
<tr>...</tr>
Etc.
I use this code:
'Get APF Number from Pipe
xNr = "20819334"
'Search Number online
Dim i As Integer
i = 0
For Each Row In APFtable
If i = 0 Then
Set Member = Row.getElementsByTagName("th")
Else
Set Member = Row.getElementsByTagName("td")
End If
i = i + 1
On Error Resume Next
If xNr = Member(4).innerText Then
'Check the corresponding checkbox
Exit For
End If
Next Row
to compare the number, in my example: xNr, to the numbers in the table. But I can't figure out how to check the box.
I tried this code:
Name = "ctl00$ContentPlaceHolderMainNoAjax$gvJumpRecordsList$ct" & 100 + i & "$chkSelect"
Set Check = IE.Document.GetElementsByName(Name)
If Not Check Is Nothing Then
Check(0).Click
End If
end all the other ways I could find in other forums to get it to work, although this came closest to working. which means that it worked when I hard-coded the name in the variable with out the "calculating position" part.
can someone advice me on my problem?
Much appreciated
Member(4).parentElement.getElementsByTagName("input")(0).Click
Should do it if the checkbox is the first input element on that row.

XPath matching text in a table - Ruby - Nokigiri

I have a table that looks like this
<table cellpadding="1" cellspacing="0" width="100%" border="0">
<tr>
<td colspan="9" class="csoGreen"><b class="white">Bill Statement Detail</b></td>
</tr>
<tr style="background-color: #D8E4F6;vertical-align: top;">
<td nowrap="nowrap"><b>Bill Date</b></td>
<td nowrap="nowrap"><b>Bill Amount</b></td>
<td nowrap="nowrap"><b>Bill Due Date</b></td>
<td nowrap="nowrap"><b>Bill (PDF)</b></td>
</tr>
</table>
I am trying to create the XPATH to find this table where it contains the test Bill Statement Detail. I want the entire table and not just the td.
Here is what I have tried so far:
page.parser.xpath('//table[contains(text(),"Bill")]')
page.parser.xpath('//table/tbody/tr[contains(text(),"Bill Statement Detail")]')
Any Help is appreciated
Thanks!
Your first XPath example is the closest in that you're selecting table. The second example, if it ever matched, would select tr—this one will not work mainly because, according to your example, the text you want is in a b node, not a tr node.
This solution is as vague as I could make it, because of *. If the target text will always be under b, change it to descendant::b:
//table[contains(descendant::*, 'Bill Statement Detail')]
This is as specific, given the example, as I can make:
//table[tr[1]/td/b['Bill Statement Detail']]
You might want
//table[contains(descendant::text(),"Bill Statement Detail")]
The suggested codes don't work well if the match word is not in the first row. See the related post Find a table containing specific text

Resources