how to check if a tag is the last one - selenium

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

Related

Find the first element matching conditions with preceding-sibling

My goal is to find the text contains in the first preceding-sibling which match.
My xpath is the following one : //*[#id='myid']/parent::td(/preceding-sibling::td/label/a)[1]
Here I want the text of the td/label/a the most close of the parent of //*[#id='myid'].
Can you tell me if you see any mistake please? Thank you a lot.
Update 1
This one is working //*[#id='myid']/parent::td/preceding-sibling::td[2]/label/a but it is not everytime 2, sometimes it can be 6 or whatever.
Update 2
Example 1
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td>
<label>
<a>text</a>
</label>
</td>
<td></td>
<td>
<input id='myid'>
</td>
</tr>
</tobdy>
Example 2
<tbody>
<tr>
<td></td>
<td>
<label>
<a>text</a>
</label>
</td>
<td></td>
<td></td>
<td></td>
<td>
<input id='myid'>
</td>
</tr>
</tobdy>
Example 3
<tbody>
<tr>
<td></td>
<td>
<label>
<a>text</a>
</label>
</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input id='myid'>
</td>
</tr>
</tobdy>
I don't think you'd need to go up to any ancestors; just go to the first preceding td (that contains a label/a)...
//input[#id='myid']/preceding::td[label/a][1]/label/a
You can use xpath below. Get first parent tr and it's child label/a
//input[#id='myid']/ancestor::tr[1]//label/a
One more way is to use union operator in xpath with all locators for all cases in order you need:
//input[#id='myid']/ancestor::td/preceding-sibling::td/label/a | //input[#id='myid']/ancestor::tr[1]/preceding-sibling::tr//label/a
Seems like table content are dynamic as i can see in your examples but <tbody> must be one for that table.
(//input[#id='myid']/preceding::td/label/a)[last()]

XSLT1 - Test if all selected nodes have a specific attribute

Given the following example xml:
<table>
<tr>
<td style="border-top:1px">
<p class="bodytext">right</p>
</td>
<td style="border-top:1px">
<p class="bodytext">left right</p>
</td>
<td style="border-top:1px">
<p class="bodytext">left</p>
</td>
</tr>
<tr>
<td style="border-top:1px">
<p class="bodytext">right</p>
</td>
<td style="border-top:1px">
<p class="bodytext">left right</p>
</td>
<td style="border-top:1px">
<p class="bodytext">left</p>
</td>
</tr>
</table>
I would like to check if ALL the TDs of the first TR have the style attribute set to border-top.
The following expression obviously does not work:
<xsl:when test="tr[1]/td/#style[contains(.,'border-top')]">
</xsl:when>
I'm using XSLT1, is there an easy way to do this?
I found the answer in this question.
Basically, this is done by getting all the nodes we want to check and compare that sequence with the filtered sequence we need.
So in my case: tr[1]/td[#style[contains(.,'border-top')]]=tr[1]/td
Thanks Dimitre!

Adding breaks in table td

<div id="headerAgentInfoDetailsPhone">
<table>
<tr>
<td>vendor:</td><td><input type="text" /></td>
<td>vendorgroup:</td><td><input type="text" /></td>
<td>account:</td><td><input type="text" /></td>
</tr>
I have code as above. Now I want a break function after the second td. For example as follows:
<div id="headerAgentInfoDetailsPhone">
<table>
<tr>
<td>vendor:</td><td><input type="text" /></td><br />
<td>vendorgroup:</td><td><input type="text" /></td><br />
<td>account:</td><td><input type="text" /></td>
</tr>
I tried to add td:n-thchild(even):after {content: \A; white-space: pre;
text-align: justify;} but its not working. The whole content is getting displayed in a single row.
A <br /> between a </td> and a <td> cannot do anything. What do you want to do by "adding a break" ?
If you want to show each info on a separated line, you have to write a new <tr> :
<table>
<tr>
<td>vendor:</td><td><input type="text" /></td>
</tr>
<tr>
<td>vendorgroup:</td><td><input type="text" /></td>
</tr>
<tr>
<td>account:</td><td><input type="text" /></td>
</tr>
</table>
It will draw a 3 lines table, with 2 cells in each line.
Yes, <tr is table row, and <td is table data. See http://www.w3schools.com/html/html_tables.asp for explanation. In a table you can't break the row with <br>, so if you use </tr> and <tr> to start a new table row instead it will work better.

vb.net How to get this inner text from webbrowser

i m trying to get some information from webbrowser control to textbox. i tried this;
WebBrowser1.Document.GetElementsByTagName("Table")
or tried this;
WebBrowser1.Document.All("table")
but i cant fin a sollution. my html cose like this;
<form id="formArea" method="post">
<table id="infoTable" cellspacing="0" cellpadding="0" border="0">
<tbody><tr>
<td class="td_sol">information 1</td>
<td class="td_sag">information 2</td>
<td class="td_sol">information 3</td>
<td class="td_sag">information 4: 123456</td>
</tr>
<tr>
<td class="td_sol">bla bla</td>
<td class="td_sag">bla bla</td>
<td class="td_sol">bla bla</td>
<td class="td_sag">bla bla</td>
</tr>
<tr>
<td class="td_sol">bla bla</td>
<td class="td_sag">bla bla</td>
<td class="td_sol">bla bla</td>
<td class="td_sag">bla bla</td>
</tr>
<tr>
<td class="td_sol">bla bla</td>
<td class="td_sag">bla bla</td>
<td class="td_sol">bla bla</td>
<td class="td_sag">bla bla</td>
</tr>
</tbody></table>
</form>
i need to get "information 4: 123456"'s only "123456" to textbox.
i have second choice for get this number. İn html another tags(8 pieces) got this number like this;
<input type="hidden" name="ImpCustomer.customerNum" value="123456">
or like this(14 pieces);
<input type="hidden" name="hCustomerNumber" value="123456">
Can anyone find a way to get this information? And maybe anyone know how can i find a tag's number in a table then a can get this to textbox?
Pls help me.
My regards.
You could get the HTML document as a string, using WebBrowser.DocumentText. Then use String.IndexOf to find <td class="td_sag">information 4:. Finally simply use String.Substring to extract the value.

Clicking on checkbox based on tables column value - IE Webdriver Selenium

Clicking on checkbox based on tables column value using Xpath. Below is the html
<table id="tblHotels">
<TBODY>
<TR>
<TH align="left">
<INPUT checkbox="" id="chkNSelectAll name=chkNSelectAll type="/>
</TH>
<TH align="left" title="Hotel">Hotel</TH>
<TH title=" align=left">
<SPAN id="spnExpandBtn">
<IMG/>
</SPAN>
</TH>
<TH align="left" title="Hotel">Hotel</TH>
<TH align="left" title="Reg Date">Reg Date</TH>
<TH align="left" title="Room Type">Room Type</TH>
<TH align="left" title="Location">Location</TH>
<TH align="left" title="Room Number">Room Number</TH>
</TR>
<TR>
<TD colSpan="11">
<IMG src=" ../NoExpiry/images/uaimBSpacer.gif"/>
</TD>
</TR>
<TR>
<TD>
<INPUT id="chkNSelect" name="chkNSelect" type="checkbox" value="on"/>
</TD>
<TD customHiddenText="">MATHEW Joe</TD>
<TD/>
<TD customHiddenText="">
<SPAN>Affray (
<STRONG/>Kim Lee)
</SPAN>
</TD>
<TD class="regDate customHiddenText=">10/01/2014</TD>
<TD customHiddenText="">1HE</TD>
<TD customHiddenText="">South West </TD>
<TD id="tdChildroom name=" tdChildroom=""/>
<INPUT id="hidYID" name="hidYID" type="hidden" value="409">
<INPUT id="hidYD" name="hidYD" type="hidden">
<INPUT id="hidYDID" name="hidYDID" type="hidden" value="1015389"/>
</INPUT>
</INPUT>
</TR>
<TR>
<TD>
<INPUT id="chkNSelect" name="chkNSelect" type="checkbox" value="on"/>
</TD>
<TD customHiddenText="">MATHEW Penny</TD>
<TD/>
<TD customHiddenText="">
<SPAN>Affray (
<STRONG/>Jim Lee)
</SPAN>
</TD>
<TD class="regDate customHiddenText=">10/01/2014</TD>
<TD customHiddenText="">1HE</TD>
<TD customHiddenText="">South West </TD>
<TD id="tdChildroom name=" tdChildroom=""/>
<INPUT id="hidYID" name="hidYID" type="hidden" value="409">
<INPUT id="hidYD" name="hidYD" type="hidden">
<INPUT id="hidYDID" name="hidYDID" type="hidden" value="1015389"/>
</INPUT>
</INPUT>
</TR>
</TBODY>
</table>
here is what i am trying and this always clicks the first checkbox??
Driver.FindElementByXPath("//td[contains(text(),'MATHEW Penny')]/preceding::td/input[#name='chkNSelect']").Click()
If i try to just find the column with the text it can find it not why it cannot find the preceding check box and jumps to first rows check box??
Driver.FindElementByXPath("//td[contains(text(),'MATHEW Penny')]
my requirement is to select the first checkbox (do something eg:add it to another table) uncheck it then check the 2nd checkbox (do something eg:add it to another table).
Use for MATHEW Penny:
//td[contains(text(),'Penny')]/preceding-sibling::td/input[#name='chkNSelect']
Use for MATHEW Joe:
//td[contains(text(),'Joe')]/preceding-sibling::td/input[#name='chkNSelect']
Its selecting all input elements with name as chkNSelect which comes before td with text as MATHEW Penny.
Use
//td[contains(text(),'MATHEW Penny')]/preceding::td/input[last()][#name='chkNSelect']
to select only first such input
You could try:
first targeting the tr
containing the td with the text node you want (using a predicate)
and then going to an input within a td in that table row
So that translates to:
Driver.FindElementByXPath("//tr[td[contains(text(),'MATHEW Penny')]]/td/input[#name='chkNSelect']")
Breakdown:
//tr[
td[
contains(text(),'MATHEW Penny')
]
]
/td/input[#name='chkNSelect']