Xpath to extract data from a table using contains - xpath

Following 2 xpaths below work fine to extract data from a table.
//*[#id="codeRow"]/td/strong[contains(text(),"Besnier")]
//*[#id="codeRow"]/td[contains(text(),"Besnier")]
I want to combine these 2 and create 1 XPATH statement that can be used as needed.
I tried using or but it did not work
ForEx:
//*[#id="codeRow"]/td[contains(descendant::*/text() , "Besnier" )]
or [contains(text(),"Besnier")]
Please advise

Try this xpath:
//*[#id="codeRow"]/td[contains(., "Besnier")]
XPath engine will convert .(current node) to string, then call function contains().
The current node and all child nodes are searched for a text node fragment "Besnier", there is no need to use an axis to select all descendants and their text nodes.

Related

Table Extraction in UIPath if table has images

I am trying to extract the Table which has the following format
When I want to extract i should have either put some character on place up icon or i dont want that either the case is fine..
But UIPath brings this way.. 78,59,237806 all as one text which is misleading.. How to resolve this issue..
Thanks
Take a look at the Find Children activity. You can specify an item via selector (the table) and use Find Children to return a collection of type UiElement.
So set the filter to extract the "<webctrl tag='tr' />" which will effectively give you a collection of the rows.
Use a For Each to iterate through each UiElement you got from the first Find Children activity, and use that element to run another Find Children. In this case, set the filter to extract the elements with a class of "mid-wrap". This gives you a collection of the elements in the row which match that requirement, and this will exclude the data-up value, since that's a different class.
You can then loop over this collection to get the innertext attribute, which will give you the actual values you're looking for each cell in the row. Use something like Add Data Row to add the values to a datatable, and let the For Each run over the next row in the collection.

XPath only get nodes from table when another node exists

I have a specific problem concerning XPath.
Say I can get a column from the table using this query:
//div[#id="someid"]/table/tbody/tr/td[9]/text()
However, I want to only get this column when another specific node exists.
I tried using:
//div[#id="someid"]/table/tbody/tr/td[9 and boolean(//a/span[#title='specifictitle'])]
This however does not work as it returns all items in the table.
I have a few specific limitations:
- //div[#id="someid"]/table/tbody/tr is static and cannot be changed.
- The td contains no other info concerning what column it is in.
Thanks in advance!
2 approaches:
First - as a direct condition within square brackets:
//div[#id="someid"]/table/tbody/tr[//a/span[#title='specifictitle']]/td[9]/text()
this approach is simpler and the position does not matter
this is also the approach that fulfills the OPs requirement, that the query should start with //div[#id="someid"]/table/tbody/tr
* You can basically put the condition [//a/span[#title='specifictitle']] to whatever element in the query you want (could also be behind tbody or table etc.)
Second - using axes (for example ancestor)
2 cases regarding the position of your element within HTML code:
1) anchor-element "before" your div with "someid":
//a/span[#title='specifictitle']//div[#id="someid"]/table/tbody/tr/td[9]/text()
2) anchor-element "after" your div with "someid":
//a/span[#title='specifictitle']/ancestor::div[#id="someid"]/table/tbody/tr/td[9]/text()
In both cases the xpath-query will not return a result if the //a/span[#title='specifictitle'] does not exist, which is what you needed, if I understood correctly

import.io : Inserting an Independent row into result using XPATH

I am trying to scrape this site using import.io: ScoreCard
I am able to get the batting scores successfully but I want to insert additional column in the end which can tell me about the innings. So it should be relative to the name of batsman.
I tried to use XPATH: //*[#id="innings_1"]/div[1]/div/h4/b
but that will always return First Inning as ID is "innings_1".
Other IDs are innings_2/3/4 etc. Is there any way in XPATH where I can get this element relative to Batsman column?
Here is what I did in order to get the desired result:
I used following XPATH value.
.//a/ancestor::div/div[1]/div/h4/b
.//a was providing me name of Batsmen. I searched for its ancestors and the path div[1]/div/h4/b was being used by only Innings section.. So it did the trick :)
Try using starts-with():
//*[starts-with(#id,'innings_')/div/div/h4/b

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.

Resources