How to get value inside the span using XPATH? - xpath

I'm trying to get value 4,023,484 from this span:
I tried this line with no luck:
//span[contains(text(),'posts')]
What am I doing wrong?

Try this one to get required value:
//span[contains(.,'posts')]/span
or
//span[normalize-space(text()[2])='posts']/span

Related

Nightwatch.js - assert.containsText I'm looking for a string but got empty (null)

Here is a spinet of my code:
browser
.waitForElementPresent('(//*[#class="textsize-xlarge"])[1]',5000)
.assert.containsText('(//*[#class="textsize-xlarge"])[1]', 'TEST-69554083978')
I want to verify that an element has a string using assert.containsText().
So I'm trying to check if the class textsize-xlarge contains text
"TEST-69554083978"
Visually I can see that it does but nightwatch returns an error message saying expected
"TEST-69554083978" got ""
Can anyone please tell me what I'm doing wrong here and point me in the right directions?
browser.useXpath(); // xpath will be used as the locating strategy so
all the selectors you pass should be xpath selectors
browser.useCss(); // switches back to css selector locator should be
your xpath
let locator = "[#class='textsize-xlarge']//*[contains(text(),'TEST-69554083978')]";
// Please modify above locator as I am not aware of your DOM but I can help further if you need any help in Xpath
return browser
.useXpath()
.waitForElementPresent(locator)
.assert.elementPresent(locator);
you can learn about node assertions: Node Assertions

Using XPATH in jmeter - based on parent

I have a xml file having the following structure:
<subscriberDetailsItem>
<externalSubId>911</externalSubId>
<paymentIndicator>2</paymentIndicator>
<IMSI>302</IMSI>
<MSISDN>416</MSISDN>
<status>A</status>
<outSubscriptionDetails>
<subscriptionDetailsItem>
<socId>ABCDEF</socId>
<featureCode>GHIJKL</featureCode>
<startDate>20180720000000</startDate>
<featureSeq>47452111</featureSeq>
<subscriptionType>DATA</subscriptionType>
<shareGroupCd>SHARE</shareGroupCd>
<initialValueBytes>1073741824</initialValueBytes>
<priority>3000</priority>
<balanceType>BAL</balanceType>
</subscriptionDetailsItem>
</outSubscriptionDetails>
</subscriberDetailsItem>
I want to be able to extract the socID field based on the externalSubId field. Essentially, I want to be able to do this:
if externalSubId=911, then, extract SOCID. Anyone has tips?
You didn't specify where would you want to extract it, so I assume you can use XPath Extractor as a post-processor to some sampler that reads that file.
You configure post-processor like this:
XPath Query can be built like this:
Select subscriberDetailsItem that has externalSubId with the value you want:
//subscriberDetailsItem[externalSubId=911]
Select socId either by using absolute path:
//subscriberDetailsItem[externalSubId=911]/outSubscriptionDetails/subscriptionDetailsItem/socId
or you can omit entities in the middle if socId cannot be found elsewhere in the item:
//subscriberDetailsItem[externalSubId=911]//socId

Java XPath with default Namespace issue

I am not able to ready node for expression
<ns:Msg xmlns:ns="http://www.noventus.se/epix1" xmlns="http:www.defaultnamespace.com">
<ns:Header>
<SubsysId>1</SubsysId>
<SubsysType>30003</SubsysType>
<SendDateTime>2009-08-13T14:28:15</SendDateTime>
</ns:Header>
</ns:Msg>
I am having this kind of xml with contains two namespaces 1 is with ns and other one is default one.
I am trying to get value for SubsysId using org.dom4j.XPath and adding namespace with
Map namespaces = new HashMap();
namespaces.put("ns", "http://www.noventus.se/epix1");
namespaces.put("main", "http:www.defaultnamespace.com");
Adding these namespaces like this
xpath.setNamespaceContext(new SimpleNamespaceContext(namespaces));
This is my expression
String expression = "/ns:Msg/ns:Header/SubsysId";
I tried multiple options but not able to get the value.
NOTE: If I remove default namespace and run then I am getting the value.
Your help is highly appreciated.
Since you defined namespaces.put("main", "http:www.defaultnamespace.com");
then you would need to specify it in your xpath.
So your xpath becomes:
String expression = "/ns:Msg/ns:Header/main:SubsysId";

simplexml_load_file with xPath returns empty array

Getting XML from this URL:
$xml = simplexml_load_file('http://geocode-maps.yandex.ru/1.x/?geocode=37.71677,55.75208&kind=metro&spn=1,1&rspn=1');
print_r($xml) shows that XML loaded, but xpath always returns empty array. I tried:
$xml->xpath('/');
$xml->xpath('/ymaps');
$xml->xpath('/GeoObjectCollection');
$xml->xpath('/ymaps/GeoObjectCollection');
$xml->xpath('//GeoObjectCollection');
$xml->xpath('precision');
Why I got empty array? Hope I just missing something easy.
It might be rather easy, but I guess it is also the most common mistake in the history of XML: You are forgetting namespaces!
A lot of elements in the given XML are changing the default namespace and you have to consider that in your XPath.
You can first register your namespace like so:
$xml->registerXPathNamespace('y', 'http://maps.yandex.ru/ymaps/1.x');
$xml->registerXPathNamespace('a', 'http://maps.yandex.ru/attribution/1.x');
and then you can query your data:
$xml->xpath('//y:ymaps/y:GeoObjectCollection');

How to get namespace names in XPath?

I have this .xml file
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3.org/TR/html4/">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:tr>
<f:td>Red</f:td>
<f:td>Yellow</f:td>
</f:tr>
</f:table>
</root>
How can i get only the elements with a specify namespace?
For example i want to retrieve only that elements in 'h' namespace.
How can i get it? In exist-db the 'namespace::' axis is not more working
Try using the in-scope-prefixes() function in a predicate:
//*[in-scope-prefixes(.)='h']
In the comments you show a solution that parses the return value from name():
//*[substring-before(name(), ":")='h']
There is a far simpler way to get all elements in the namespace that's mapped to the h prefix:
//h:*
Note: when I first tested this, I was getting back all elements in the document. That's because both of your prefixes are mapped to the same namespace:
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3.org/TR/html4/"
You should also fix this.

Resources