xpath. Select element if brother value - xpath

<tr>
<td>AAA</td>
<td>
<a class="btn" role="button"> </a>
<td>
</tr>
<tr>
<td>bbb</td>
<td>
<a class="btn" role="button"> </a>
<td>
</tr>
How to select a if tr contains <td>AAA</td>. Perfect if I can do it with XPath. But maybe another way in WebDriver

You can do this using Xpath.
//td[text() = 'AAA']/following-sibling::td/a
This will get find a td which has the text AAA. Then it will select the following td sibling and get the a from this td.

Related

Get <td> cell value using jQuery without specifying specific class or id

I am using ERB templating in Sinatra to display a table of data.
How can I use jQuery to get table data of a specific row when I click the EDIT button without having to assign specific class or ids to each <td> or <tr>?
<table>
<tr>
<td>question</td>
<td>answer</td>
<td>edit</td>
</tr>
<tr>
<td>What is a carrot?</td>
<td>A vegetable</td>
<td>
<a id="editbutton" class="btn btn-default" onclick="editModal()" data-toggle="modal" data-target="#basicModal4" aria-hidden="true" name="btn" data-modal-type="confirm"><em class="fa fa-pencil"></em></a>
</td>
</tr>
<tr>
<td>What does HTML stand for?</td>
<td>Hyper Text Markup Language</td>
<td>
<a id="editbutton" class="btn btn-default" onclick="editModal()" data-toggle="modal" data-target="#basicModal4" aria-hidden="true" name="btn" data-modal-type="confirm"><em class="fa fa-pencil"></em></a>
</td>
</tr>
</table>
UPDATE:
I have managed to get the following to get the value in the nearest <td>
$('.editbutton').click(function(e) {
var output = $(this).closest('tr').find('.as').text();
alert(output);
});
but I need values from both table data cells in that row. Any ideas?
Jquery .closest() , check API https://api.jquery.com/closest/

Selecting the Parent of the Parent element. Tables/Watir/Ruby

Hey i am trying to select the parent of the parent element i suppose. I have a table in which i am trying to use Watir/Ruby to select the edit or delete button from the same row but in a different coloumn, example.
Name 1 -> Edit Icon -> Delete Icon
Name 2 -> Edit Icon -> Delete Icon
Example code below:
<html>
<body>
<table class="table-responsive">
<thead>...</thead>
<tbody>
<tr>
<td class="col-name">Name 1</td>
<td>
<a class="edit" href="/">
<span class="icon-edit"></span>
</a>
<a class="delete" href="/">
<span class="icon-delete"></span>
</a>
</td>
</tr>
<tr>
<td class="col-name">Name 2</td>
<td>
<a class="edit" href="/">
<span class="icon-edit"></span>
</a>
<a class="delete" href="/">
<span class="icon-delete"></span>
</a>
</td>
</tr>
<tr>
...
</tr>
<tbody>
</table>
</body>
</html>
So far i have tried this but it is not working. I get an error TypeError: can't convert Hash into an exact number
table = #browser.table.when_present(:class => "table-responsive")
iconrow = table.span(:text => "Name 1").parent
if iconrow.a(:class => "edit").exists?
iconrow.a(:class => "edit").click
end
There might be a couple issues with the code:
* The :class locator is being applied to when_present rather than the table itself.
* The element with "Name 1" is a td rather than a span.
Try:
table = #browser.table(:class => "table-responsive").when_present
iconrow = table.td(:text => "Name 1").parent
if iconrow.a(:class => "edit").exists?
iconrow.a(:class => "edit").click
end

XPATH (Scrapy): select text between 2 certain keywords

I'm trying to extract text between 2 keywords 商品詳細 and 支払詳細 in this HTML
<TR>
<TD BGCOLOR=#336600><BR></TD>
<TD COLSPAN=3 BGCOLOR=#FFFFCC><FONT COLOR=#336600 SIZE=4><B> 商品詳細 </B></FONT></TD>
</TR>
<TR>
<TD COLSPAN=4 HEIGHT=10>
<LI STYLE=><SPAN STYLE=>鍵付きで盗難を防止できます。</SPAN>
<LI STYLE=><SPAN STYLE=>商品サイズ:約28*36*12cm</SPAN>
<LI STYLE=><SPAN STYLE=>素材:鉄製</SPAN>
<LI STYLE=><SPAN STYLE=>※柄は、ランダムにて発送なります</SPAN>
<LI STYLE=><SPAN STYLE=></SPAN>
<LI STYLE=>
<SPAN STYLE=></SPAN>
</TD>
</TR>
<TR>
<TD><BR></TD>
<TD COLSPAN=2 ALIGN=left><BR></TD>
<TD><BR></TD>
</TR>
<TR>
<TD COLSPAN=4 HEIGHT=25><BR></TD>
</TR>
<TR>
<TD BGCOLOR=#336600><BR></TD>
<TD COLSPAN=3 BGCOLOR=#FFFFCC>
<FONT COLOR=#336600 SIZE=4><B> 支払詳細 </B></FONT>
</TD>
</TR>
I tried the solutions in these 2 links but they didn't work for me
Scrapy xpath between 2 keywords
Xpath text extraction between 2 keywords
This is the result I have when run in scrapy shell:
In [21]: response.xpath("//text()[preceding-sibling::*[text()='商品詳細'] and following-sibling::*[text()='支払詳細']]").extract()
Out[21]: []
With xpath you can navigate the document in any direction,so in this case you want to find a key node that you know some info about and navigate to related nodes.
//td[contains(.//text(),'商品詳')] # find td that contains some text
/../following-sibling::tr//li/span/text()" # find text in it's father's sibling
I've tried this in a shell:
>[1]: sel.xpath("//td[contains(.//text(),'商品詳')]/../following-sibling::tr//li/span/text()").ex
tract()
<[1]: ['鍵付きで盗難を防止できます。', '商品サイズ:約28*36*12cm', '素材:鉄製', '※柄は、ランダムにて発送なります']

Selenium Webdriver and Xpath:Complicated selection

So my structure looks like this at some point. I need to select text ShapeCSR having "select" link using xpath. Both these text present in the same line.
<td>
<img class="imHeader" alt="" src="include/img/context/level_dash.gif"></img><img class="imHeader" alt="" src="include/img/context/icon_telco_level.gif"></img>
ShapeCSR
</td>
<td>
…
</td>
<td>
<a id="lnk_LEVEL_2010" href="jfn?isLevel=true&level=L4%3A2010&mfunc=614&cfunc=615&oid=L4%3A2072&ctx=L&jfnRC=5">
Select
</a>
</td>
Your question is not quite specific enough but I'm assuming you have a <table> of <tr> where you want to select the <td> containing "ShapeCSR" when the <tr> contains a <td> with the text "Select" in it.
Try this:
WebElement yourLink = webDriver.findElement(By.xpath("//tr[td/a[contains(text(),'Select')]]/td[contains(text(),'ShapeCSR')]"));

how to determine xpaths for ajax element

I need to detemine xpath for element mainForm:queryConfigure:fetchReport.
<span id="mainForm:queryConfigure:j_id18">
<table id="mainForm:queryConfigure:j_id19"
class="showReportTable" align="center">
<tbody>
<tr>
<td>
<input id="mainForm:queryConfigure:fetchReport" type="image"
src="images/show_report.gif" name="mainForm:queryConfigure:fetchReport"/>
</td>
</tr>
</tbody>
</table>
</span>
I tried
selenium.click("//input[#id='mainForm:queryConfigure:fetchReport'][#type='image'][#src='images/show_report.gif']");
and
selenium.click("//input[#id='mainForm:queryConfigure:fetchReport']");
One more case:
<div class="tabUnselectedText" align="center">
Notifications
</div>
Id and name attribute values are acceptable locators for method click. See locating elements in the documentation.
selenium.click('mainForm:queryConfigure:fetchReport');

Resources