How to XPATH for Table element which have dynamic Ids - xpath

I have to get the XPATH for dynamic Ids . The code look like this
<table width="100%" role="presentation">
<tbody>
<tr>
<tr>
<td id="DWT10" role="presentation">
<div id="zl__TV-main__rows" class="DwtListView-Rows" style="height: 130px; width: 377px;">
<div id="zli__TV-main__654" class="RowDouble RowEven " role="treeitem" tabindex="0" aria-label="Unread, hello everyone, Wilkerson, 12:26 AM" aria-posinset="1" aria-level="1">
My XPATH goes like this :
//div[starts-with(#id='zl__CLV-main') and ./div [contains(#aria-posinset,'1')]]
I am getting:
Could not evaluate XPATH error.

XPAth only works if you have a valid XML source file. In your example there are no closing tags.
Your XPATH is invalid. the start-with function must be changed to
//div[starts-with(#id,'zl__CLV-main')
comma instead of =

Related

How to use XPath to select following-sibling

<tr>
<td width="120" align="right" class="tit">application:</td>
<td style="width:345px;word-break:break-all;">
<a style="text-decoration: underline; color: #0066ff; cursor: pointer"
href="javascript:_search('pa', '<font color=red>PartA</font>PartB');"> <font color=red>PartA</font>PartB</a>;
</td>
</tr>
<tr>
In above code, I use part of the name (Part A) to search result, how can I get the whole name which combines PartA and PartB. I use below code and just get PartA and nothing
html.xpath('//td[contains(text(),"application")]/following-sibling::td[1]/a/font/text()')[0]
html.xpath('//td[contains(text(),"application")]/following-sibling::td[1]/a/text()')[0]
You need to fix your second XPath accordingly :
//td[contains(text(),"application")]/following-sibling::td[1]/a/text()[normalize-space()]
Output : Part B
To select the two items directly you can use :
//td[contains(text(),"application")]/following-sibling::td[1]//text()[normalize-space()]
To combine them :
concat(//td[contains(text(),"application")]/following-sibling::td[1]/a/font/text(),//td[contains(text(),"application")]/following-sibling::td[1]/a/text()[normalize-space()])
or
string(//td[contains(text(),"application")]/following-sibling::td[1]/a)
Output : PartAPartB

regex using dynamic input in Jmeter(regex extractor)

I have a query regarding Jmeter regex extractor. I am trying to implement 1 scenario however not able to do same. Below are the details:
Requirement :
In Jmeter I have defined user defined variable : String VAR = KZ
now I am trying to use Regex extractor so that from the HTML response, regex will match VAR value in HTML(defined below) and will fetch span class name, as I need to set checkbox ON for KZ.
Requirement is to handle checkbox ON functionality through user defined variable, that means I don't want to hardcode class name instead based on user defined variable(which will be td value i.e. in this example KZ) I have to fetch class name using Regex Extractor. Could someone please help how to proceed?
Below is HTML Code:
<tr class="trClass">
<td style="width: 13.5%;">
<span class="checkbox"><input id="ctl00ctl94" type="checkbox" name="$ctl95$"
onclick="return validatecheck();" /></span>
</td>
<td style="width: 41.2%;"> KZ </td>
<td style="width: 0%; display: none;"> 5581357 </td>
<td style="width: 32%;"> 06/03/2018 2:22:38 PM </td>
</tr>
<tr class="trClass">
<td style="width: 13.5%;">
<span class="checkbox"><input id="ctl00ctl95" type="checkbox" name="$ctl95$"
onclick="return validatecheck();" /></span>
</td>
<td style="width: 41.2%;"> TM </td>
<td style="width: 0%; display: none;"> 5581358 </td>
<td style="width: 32%;"> 06/03/2018 2:22:38 PM </td>
</tr>
<tr class="trClass">
<td style="width: 13.5%;">
<span class="checkbox"><input id="ctl00ctl96" type="checkbox" name="$ctl96$"
onclick="return validatecheck();" /></span> </td>
<td style="width: 41.2%;">TR </td>
<td style="width: 0%; display: none;"> 5581359 </td>
<td style="width: 32%;"> 06/03/2018 2:22:38 PM </td>
</tr>
Using regular expressions for parsing HTML is not the best idea as:
they are hard to develop and/or maintain
they are very sensitive to markup change hence fragile, i.e. if order of attributes changes or something will go to a new line - it will simply ruin your regex
So I would recommend going for another post-processor which can work with DOM directly, for instance XPath Extractor
The relevant XPath query which will fetch the classname of span which is above the KZ text would be something like:
//td[contains(text(),'KZ')]/preceding::*/span/#class
Of course you can substitute KZ with the JMeter Variable reference, i.e.
//td[contains(text(),'${VAR}')]/preceding::*/span/#class
However you will not be able to test your queries using XPath Tester mode of the View Results Tree listener, you will have to go for Debug Sampler instead to visualize the resulting variable.
Check out XPath Tutorial and Using the XPath Extractor in JMeter guide to get familiarized with XPath language.
Also be aware that according to JMeter project main page:
JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages.
So I don't believe fetching the span classname will solve your problem, most probably you will need to send underlying input name as a parameter so you should be looking for
//td[contains(text(),'KZ')]/preceding::*/span/input/#name

Getting href by looking for another tag

I'm trying to extract href links from a web page that has multiple rows like below but I only want the a href of the ones that have the <b> block
<tr bgcolor="#ffffff">
<td>
<a href="?6384593.html" style="background-color: transparent;">
<span class="ts">
<font size="1">
<font color="#006633">
</font>
</font>
<b>Lee Swanson Research Update</b>
<font color="#7777CC"> - Swanson Health Products</font></span>
</a>
</td>
</tr>
In this case I use the xpath expression "//b" to find the bold tag, but what I specifically want is the a href link. Is this possible with xpath?
//b/ancestor::a[1]/#href
Try something like the above
The below XPATH will work :
//a[.//*[local-name(.)='b']]/#href
UPDATE AS #Jens Erat suggested.
//a[.//b]/#href

Selenium IDE: How to detect the xpath's if div id's , class are randomly generated every time?

I am trying to detect the xpath or css but everytime I run the script, the div id's and class names change which there by fails the script.
<div class="yui-dt-bd" style="height: 300px; width: 100%;">
<table id="yuievtautoid-0" summary="" style="margin-top: 0px;">
<tr id="yui-rec28" class="yui-dt-rec yui-dt-first yui-dt-even yui-dt-selected" style="">
<td id="yui-gen52" class="yui-dt23-col-professorId yui-dt-col-professorId yui-dt- sortable yui-dt-first" headers="yui-dt23-th-professorId ">
<div id="yui-gen51" class="yui-dt-liner">1</div>
</td>
<td id="yui-gen44" class="yui-dt23-col-professorName yui-dt-col-professorName yui-dt-sortable yui-dt-last" headers="yui-dt23-th-professorName ">
<div id="yui-gen43" class="yui-dt-liner">John Power</div>
</td>
</tr>
</table>
</div>
I had written xpath=//*[#id="yui-gen46"], but id's keep changing. Tried writing table id too. But it does not work.
xpath=id('yuievtautoid-1').
Appreciate some input .
You can specify a part of class or id that is not changed. For example:
//*[contains(#class, 'col-professorName')]
or
//*[contains(#id, 'yuievtautoid')]
or CSS versions:
css=*[class*="col-professorName"]
css=*[id^="yuievtautoid"]

getting value using xpath, ruby

I need to get value 9,70 from the following code, but am unable to do so. The number's comma is part of number and not delimiter, so the whole number is needed in one string. id="cheapest wine" is unique, but it keeps returning error.
<tr class="chartTableHeader">
<tr class="chartTableRow">
<td class="chartTableColFirst" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
<span id="cheapest wine">9,70</span>
</td>
<td class="chartTableCol" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
Using Nokogiri, and assuming that your html is formatted properly, you can get the value as follows:
require 'nokogiri'
xml = <<-EOF
<root>
<span id="cheapest wine">9,70</span>
</root>
EOF
doc = Nokogiri::XML(xml)
doc.xpath('//span[#id="cheapest wine"]').map do |add|
puts add.inner_text
end
Here the key is the XPath query: //span[#id="cheapest wine"] which searches for the span nodes whose id is "cheapest wine" (being an id, there should only be one).
Use the following XPath expression:
number(
translate(tr[#class='chartTableRow']/td/span[#id='cheapest wine'],
',',
'.'
)
)
where the current node from which the XPath expression is evaluated is the parent of the XML fragment shown in your question.
The XPath expression above evaluates to 9.7

Resources