Exclude inner tag html using XPATH - xpath

I have a HTML
......<div class="p_value">Resale
Get Documents Verified
</div>....
The expected output: Resale
My incorrect output: Resale Get Documents Verified
My formula: =IMPORTXML(A2,"//*[normalize-space(text()) = 'Transaction type']/following-sibling::div")
How do I exclude

You can try this.
//div/text()
or
//div[#class="p_value"]/text()

Related

How to take xpath to Get Text from class inside th

I have the following XPath :
//table[#class='ui-jqgrid-htable']/thead/tr/th//text()
And I'm trying to get the text from it with the following command :
String LabelName = driver.findElement(By.xpath("//table[#class='ui-jqgrid htable']/thead/tr/th//text()")).getText()
But it's not printing text, the result is blank. Could you help me please ?
The text() in your xpath does not qualify as an element. Your element ends at //table[#class='ui-jqgrid-htable']/thead/tr/th. Try using getText() for this XPath.
Also, a table would have many headers. Using findElement will only return the first one.
If you want to get all headers use
driver.findElements(By.xpath("//table[#class='ui-jqgrid-htable']/thead/tr/th"))
and loop through the list to getText of individual element.

JMeter XPath getting multiple values and odd output order

Preamble
I have a JMeter script with an XPath Extractor, in this I have specified a query that gets multiple values from the XML document. This all works fine
XML
<?xml version="1.0" encoding="UTF-8"?>
<InventoryAvailabilityAdvice>
<Warehouse>WFC2</Warehouse>
<Timestamp>2019-07-31T23:00:02.177</Timestamp>
<InventoryItem>
<ItemNumber>80903</ItemNumber>
<AvailableQuantity UnitOfMeasure="EA">13</AvailableQuantity>
</InventoryItem>
<InventoryItem>
<ItemNumber>80901</ItemNumber>
<AvailableQuantity UnitOfMeasure="EA">17</AvailableQuantity>
</InventoryItem>
</InventoryAvailabilityAdvice>
Problem
When I then try to get these values in a loop using a JSR232 Sampler they don't seem to come out in the order declared in the XPath Query.
I expected theData_2 to contain the UnitOfMeasure attribute and theData_3 to contain the quantity, but as you can see they are reversed.
Question
Is this expected behavior? If so, when an element has multiple attributes how do I know which order those will be made available as?
Thanks
The order of XPath nodesets produced by union operator is not guaranteed, you can see putValuesForXPathInList() function for implementation details
Actually if you've decided to go for Groovy - you don't even need the XPath Extractor, you can use XmlSlurper class for parsing the XML response.
Example code:
def response = new XmlSlurper().parseText(prev.getResponseDataAsString())
response.InventoryItem.eachWithIndex { item, index ->
log.info('Item: ' + index)
log.info('ItemNumber: ' + item.ItemNumber)
log.info('AvailableQuantiry: ' + item.AvailableQuantity)
log.info('UnitOfMeasure:' + item.AvailableQuantity.#UnitOfMeasure)
}
Demo:
References:
Groovy: Processing XML
Apache Groovy - Why and How You Should Use It

I'm trying to get the "id" of an html tag, but the xpath doesnt work

I'm using selenium and i want to get the "id" of an html tag with "find_elements_by_xpath", but i've got this error :
selenium.common.exceptions.InvalidSelectorException: Message: invalid
selector: The result of the xpath expression
"//body[contains(#class,'de')]/div/div[contains(#class,'container-fluid
default')]/section[contains(#id,'mainContent')]/div[contains(#class,'row-fluid')]/div[contains(#id,'contentContainer
row-fluid')]/div[contains(#class,'content')]/div[contains(#class,'ses')]/ul/li/#id"
is: [object Attr]. It should be an element.
When i executed this code:
browser.find_elements_by_xpath("//body[contains(#class,'de')]/div/div[contains(#class,'container-fluid default')]/section[contains(#id,'mainContent')]/div[contains(#class,'row-fluid')]/div[contains(#id,'contentContainer row-fluid')]/div[contains(#class,'content')]/div[contains(#class,'ses')]/ul/li/#id")
While the same code without "/#id" work perfectly but i've got only the text in the "li" tag and it's not what i want.
According to the error, the problem comes from the Xpath.
I expected that this code would return all the "id" that are in "li" html tag, but i got the error.
Thank you for your help
#id is an attribute, not an element. The XPath is OK, but the function only returns elements, not attributes. I doubt there's find_attributes_by_xpath, but if you want to find the li element that has the #id defined, you can specify that in the quantifier:
browser.find_elements_by_xpath("//body[contains(#class,'de')]
/div/div[contains(#class,'container-fluid default')]
/section[contains(#id,'mainContent')]
/div[contains(#class,'row-fluid')]
/div[contains(#id,'contentContainer row-fluid')]
/div[contains(#class,'content')]
/div[contains(#class,'ses')]/ul/li[#id]")
~~~~~
You can then call element.get_attribute('id') to retrieve the id of the element.

get only a part of text() with xpath

I'm trying to get the array of authors of this website:
http://www.intechopen.com/books/latest/1/list
with this xpath:
response.xpath("//div[#id='sizer']/div[#id='content']/div[#class='grid']/div[#class='main-content']/div[#id='tc']/div/ul[#class='book-listing entity-listing']/li/dl/dd[#class='meta']/text()[count(preceding-sibling::br) = 0]").extract()
but i want only the names, without the "editor", how can I do it?
After selecting the text, use the regular expression function re() with a capture group in order to exclude the text you do not want:
response.xpath("//div[#id='sizer']/div[#id='content']/div[#class='grid']/div[#class='main-content']/div[#id='tc']/div/ul[#class='book-listing entity-listing']/li/dl/dd[#class='meta']/text()[count(preceding-sibling::br) = 0]")
.re(r'Editor\s*(.*)')

XPath Get value inside query string

I use this XPath
//#getparam
to get this list of URL
/blabla/bla.php?id=100001&target=lala&new=lulu
/blabla/bla.php?id=100002&target=lala&new=lulu
/blabla/bla.php?id=100003&target=lala&new=lulu
I want to get list of IDs, and try this XPath
substring-before(substring-after(//#getparam, "?id="), "&")
but it only returns first ID. How to get all IDs only using XPath?
If you're using XPath 2.0, the following will do the trick:
//#getparam/substring-before(substring-after(., "?id="), "&")
If you're using XPath 1.0, there is no single expression to return the desired result set. You can iterate over each result of //#getParam and apply the functions.

Resources