Selecting checkbox from the line that contains word Capybara - ruby

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

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!

XPath of the edit button is //*[#id="edit_1088"]

My query is I need to click on the first edit link on the table. The table has 7 columns and rows will be incremented dynamically.
HTML Code for first row:
<tr role="row" class="odd">
<td>
<form method="GET" target="_blank" action>...</form>
<td> ALLOCATION CHANGE</td>
<td class="right"></td>
<td> SATTER, KRAIG</td>
<td> CAFEMANAGER1</td>
<td class="sorting_1">03/08/2016 17:00</td>
<td class="edit_icon" id="edit_1088" onclick="on EditClick(1088)">
<span class="view_icon" style="margin-left: 40%;"></span>
</td>
</tr>
Note: the ID of the edit button keep on changing as the row increments.
Mycode in cucumber -ruby -capybara
And /^I click on the Expresso image$/ do
find(:xpath, '//*[#id="l1row"]/span').click
find('tr:odd > td:edit_icon [id="edit_"] match: first').click
sleep 10
end
Error Message: invalid selector: An invalid or illegal selector was specified
Update based on the posted HTML -
Note: Your first <td> isn't closed, I'm assuming thats just an error when you were adding the HTML to the question.
So from the HTML posted you don't actually have an edit link you just have a td you need to click on - the one in the first row with an id beginning with "edit_" so
find('tr:first-child > td[id^="edit_"]').click
The attempt you posted in your question won't work because there is no such CSS selector as odd or edit_icon and find needs valid CSS selectors (or XPath if you specify XPath or set it as your default)
Previous Answer Based on the wording of the question:
If this is a table then you can do what you want with CSS and not worry about XPath.
find('tr:first-child > td:last-child [id^="edit_"]')
will find the element whose id starts with "edit_" in the last td in the first row. If your rows and columns are not actually a table you'll need to provide some example HTML of what you're talking about.

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