How to find element xpath if they have no any attribute values? - xpath

For an example
<tr><th>sample1</th><th>sample2</th></tr><tr<td>data1</td><td>data2</td></tr>
In this above table we have no any attribute values. But they only have TagName. From that how we can find the unique xpath for all of them.I try using xpath like
//tr[not(#class) and not (#id)]
But by using it they can fetch all tr .so from that how we can find the unique xpath. Please can anybody know the answer update immediately with some demo...

Attribute values are handy but not required to select data in XPath.
For example, for your XML, formatted for legibility and fixed to be well-formed,
<table>
<tr>
<th>sample1</th>
<th>sample2</th>
</tr>
<tr>
<td>data1</td>
<td>data2</td>
</tr>
</table>
each of the following XPath expressions,
via position: /table/tr[2]/td[2]
via content: //td[.='data2']
via adjacency: //td[.='data1']/following-sibling::td[1]
via heading: //tr[2]/td[count(../../tr/th[.='sample2']/preceding-sibling::th)+1]
would select
<td>data2</td>
without relying upon attribute values.
See also
How to select table entry via XPath

Related

What XPath expression is able to fetch rows from a table regardless of presence of implicit tbody tag in DOM? [duplicate]

As in this Stack Overflow answer imagine that you need to select a particular table and then all the rows of it. Due to the permissiveness of HTML, all three of the following are legal markup:
<table id="foo"><tr>...</tr></table>
<table id="foo"><tbody><tr>...</tr></tbody></table>
<table id="foo"><tr>...</tr><tbody><tr>...</tr></tbody></table>
You are worried about tables nested in tables, and so don't want to use an XPath like
table[#id="foo"]//tr.
If you could specify your desired XPath as a regex, it might look something like:
table[#id="foo"](/tbody)?/tr
In general, how can you specify an XPath expression that allows an optional element in the hierarchy of a selector?
To be clear, I'm not trying to solve a real-world problem or select a specific element of a specific document. I'm asking for techniques to solve a class of problems.
I don't see why you can't use this:
//table[#id='foo']/tr|//table[#id='foo']/tbody/tr
If you want one expression without node set union:
//tr[(.|parent::tbody)[1]/parent::table[#id='foo']]
In XPath 2.0, the optional step can be expressed as (tbody|.).
//table[#id="foo"]/(tbody|.)/tr
XPathTester.com demo
The pipe (|) denotes union (of two node-sets), the dot (.) denotes identity step (returning just what the previous step did).
This can be expanded to include more optional elements at once:
//table[#id="foo"]/(thead|tbody|tfoot|.)/tr
Use:
//table[#id="foo"]/*[self::tbody or self::thead or self::tfoot]/tr
|
//table[#id="foo"]/tr
Select any tr element that is a child of any table that has an id attribute "foo" or any tr element that is a child of a tbody that is a child any table.

Show only number in laravel

I have a data in my database, which contains number and text both like [ 4,First Appeal ].Now fetch the data and want to show only the TEXT not the NUMBER part.How can I do
<tr>
<td><strong>Case type :</strong>{{ $case_details->case_type }}</td>
</tr>
Thanks in advance....
You can use explode on a string if it is always going to be comma separated. I would probably add some checking as the [0] could fail if there is no comma in the data. Secondly, I would suggest you revaluate how you are storing your data as it seems like this structure is not very efficient for multiple reasons. One suggestion would be be to only refer type to a number which has a config on the server which has the corresponding First Appeal or maybe a separate table of types with a foreign key to the types on case_details.
<tr>
<td><strong>Case type :</strong>{{ explode(",", $case_details->case_type)[0] }}</td>
</tr>

XPath concatenate table row cells

I am trying to extract the concatenated cells from a HTML table for each row using XPath. For example, if I have a table like
<table>
<tr><th>FirstName</th><th>LastName</th><th>Title</th></tr>
<tr><td>First1</td><td>Last1</td><td>Title1</td></tr>
<tr><td>First2</td><td>Last2</td><td>Title2</td></tr>
<tr><td>First3</td><td>Last3</td><td>Title3</td></tr>
</table>
I want to extract this data so that I get the full name of the person in each row
First1 Last1
First2 Last2
First3 Last3
I can get each column separately and then merge them in my code later, but prefer to get this done in a single XPath query. I have tried to use concat, but can't figure out where to use the concat.
Thanks in advance.
The concatenation you tried only concats the xpath, not the nodes. If you want to select more than one nodes, you should use | between them.
//tr//td[1] | //tr//td[2]

better selenium xpath is expecting

I'm trying to create xpath expression which will work with selenium using following html snippet.
Below is table contains various row that gets incremented with uniquely generatedid(for example in following snippet that id is 1000).
Selenium has created following expressions when row of id 1000 was added in table. However instead of using id, I want to create xpath by using 3rd data element in row which is (MyName) in html snippet.
A possible suggestion is to not use xpath whenever possible.
http://saucelabs.com/blog/index.php/2011/05/why-css-locators-are-the-way-to-go-vs-xpath/
You need to convert the places in the XPATH where it is referring to the row by its ID to its relative position in the table.
In all of your XPATHs, you would change tr[#id='1000'] to tr[3]
Your first example XPATH would look liek this:
//tr[3]/td[1]/a[1]/img //tr[#id='1000']/td[1]/span/a/img
Your second example would follow similarly:
//tr[3]/td[1]/span/a/img
As would your third:
//tr[3]/td[1]/a[2]/img
Hopefully you are now able change the rest of them.

why wouldn't this XPath example work?

I am trying to locate the first <th> element under a <table> element. The table element is tagged with a particular id, and is locatable when I only look as far as that tag.
But when I try to go a little further down and search using the XPath below, it returns a null element. The '/th[0]' is to say: return the first <th> element, under the element that is tagged with the particular id.
In the example, the id value is populated prior to the search:
"//*[#id='{0}']/th[0]"
XPath indexes are 1-based. Try: //*[#id='{0}']/th[1]
This trips me up all the time as well; too much time spent with 0-based indexing in C, C++, etc.

Resources