better selenium xpath is expecting - xpath

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.

Related

Get an onRun cell text and use as parameter in a filter

Currently I am learning jmeter and using it to test a reporting tool
This portal I am testing contains a table that as default, brings all the data the moment the user access the view.
My test needs to access this view, capture a value (any value) from a specific column (and any row) and then search for this value.
I was checking how to capture this on run parameter with Jmeter and found this:
https://www.blazemeter.com/blog/how-to-retrieve-database-data-for-api-testing-with-jmeter
but as I understood, I would need access to the database in order to do it, which I dont.
Any idea of how to retrieve this?
Thanks!
JMeter provides a number of Post-Processors which can be used for extracting data from the table.
If you're talking about normal HTML Table - the most suitable would be XPath Extractor, the relevant XPath query would be something like /table/tr/td/text()
So if you add XPath Extractor and configure it like
You will be able to use random cell text as ${value} where required
More information:
XPath Tutorial
Using the XPath Extractor in JMeter

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: Forms with Unique IDs

I am trying to use XPath as part of a data scraper in order to scrape random comments from reddit for a project. The problem is, the comment forms have unique IDs that change on every page and within comment indent levels. I'm not sure how to make XPath target all of the comment fields with these different IDs.
An example is shown below:
//form[#id='form-t1_cj8cyupxa3']/div
//form[#id='form-t1_cj8e0iyx6w']/div
If there is some pattern to the id then try e.g. //form[starts-with(#id, 'form-')]/div

Xpath to extract data from a table using contains

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.

iReport + XPath - Sum String Column

I'm building a report where I have a subreport. This subreport brings some data through XPath, this means it's everything strings from a XML. One of the columns of this subreport has some values, where I need do sum them and show in the end of the table.
I don't know how to put this to work. I've tried to create a variable with sum parameter, but it does not work.
Did anybody need this before?
Scenario:
I load a lot of values from a XPath query, e.g:
/something/something1/something2
This query returns some fields according to my needs. With them I build a table (in a subreport). The problem is: the last column (4), values are strings from XML.
iReport version: 3.0.0
Really thanks!
Solution:
What I needed? According to the original post, a column with strings read from a XML through XPath and a footer with the sum.
Result from column SUM
What to do? Create a variable where you'll keep your sum. After created, edit it. e.g:enter image description here
Fill variable expression with the format you need. Here I used
new BigDecimal($F{theFieldToSum});
Click ok. Now the variable is created, you must create a new TextField where you'll show your sum. Create it and edit it. Here I used the following format:
new java.text.DecimalFormat("#,##0.00").format($V{theVariableYouCreatedBefore})
Click "Apply" and that's all. Compile your report and now you'll have the expected result. For sure, you can do some adapt, but in general this is the process.

Resources