This question already has answers here:
Difference between "//" and "/" in XPath?
(4 answers)
Closed 4 months ago.
I have following XML:
<article>
<div class="class1">
<span>Article header 1</span>
<div>
<span>Date</span>
</div>
</div>
<div class="class2">
<span>Details</span>
<div class="class3">
<span>Number</span>
</div>
</div>
<div>
<span>Price 1</span>
</div>
<div class="class3">
<span>Footer 1</span>
<div>Footer details</div>
</div>
</article>
<article>
<div class="class1">
<span>Article header 2</span>
<div>
<span>Date</span>
</div>
</div>
<div>
<span>Price 2</span>
</div>
<div class="class2">
<span>Details</span>
<div class="class3">
<span>Number</span>
</div>
</div>
<div class="class3">
<span>Footer 2</span>
<div>Footer details</div>
</div>
</article
And I want to select only DIV without class and only from first nesting level
In this case
<div>
<span>Price 1</span>
</div>
and
<div>
<span>Price 2</span>
</div>
Note that this div in first article is on 3 place but in second article is on 2 place
I tried to use
//div[not(#class)]
but it find all elements in article, not only from first nest
You were close to the right expression.
This will select what you are looking for:
article/div[not(#class)]
This article/div indicates the direct div child element of the top node article.
Related
I try to extract all links based on these three conditions:
Must be part of <div data-test="cond1">
Must have a <a href="..." class="cond2">
Must not have a <img src="..." class="cond3">
The result should be "/product/1234".
<div data-test="test1">
<div>
<div data-test="cond1">
Link 1
<div class="test4">
<div class="test5">
<div class="test6">
<div class="test7">
<div class="test8">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div data-test="test2">
<div>
<div data-test="cond1">
Link 2
<div class="test4">
<div class="test5">
<div class="test6">
<div class="test7">
<div class="test8">
<img src="bild.jpg" class="cond3">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I'm able to extract the links with the following xpath query.
//div[starts-with(#data-test,"cond")]/a[starts-with(#class,"cond")]/#href
(I know the first part is not really neccessary. But better safe than sorry.)
But I'm still struggling with excluding the links containing an descendant img tag and how to add it to the query above.
This should do what you want:
//div[#data-test="cond1" and not(.//img[#class="cond3"])]
/a[#class="cond2"]
/#href
/product/1234
I have a xml like this and am trying to select the groupIdentifier element without the display:none child (would like to use the css "identifier" along with it) to finally select the input. Have been at this for hours and would like to call the xpath gods to help me out.
<div class="groupIdentifier">
<div>
<input class="inputClassIdentifier">
</div>
<div>
...
<div>
<div class="something">
... some more elements
</div>
<div class="identifier hidden" style="display: none">
... some more elements
</div>
<div class="something">
... some more elements
</div>
</div>
</div>
</div>
<div class="groupIdentifier">
<div>
<input class="inputClassIdentifier">
</div>
<div>
<div>
<div class="something">
... some more elements
</div>
<div class="identifier ">
... some more elements
</div>
</div>
</div>
</div>
Thanks
edit:
I have
//div[contains(#class, 'identifier') and not(contains(#style, 'display: none'))] which basically selects the identifier div of the second section.
What I need now is to select the input with class inputClassIdentifier within its parent.
Here's your xpath.
//div[#class='groupIdentifier' and div/div/div[not(contains(#style, 'display: none'))]]
I got it using descendant axis
//div[#data-testid='groupIdentifier' and descendant::div[contains(#class, 'identifier') and not(contains(#style, 'display: none'))]]//input[#name='inputClassIdentifier']
<div class="a">
<div class="a random number of div wrapers">
<div>Random1<em>Median</em>
<div class="b">
<div class="c">Edit</div>
</div>
</div>
<div>Random2<em>Median</em></div>
<div>
<em>Median</em>
</div>
<div>Random3<em>Median</em></div>
<div>Random4<em>Median</em>
<div>Random4<em>Median</em></div>
</div>
</div>
<div class="a">
<div class="a random number of div wrapers">
<div>Random1<em>Median</em></div>
<div>Random2<em>Median</em></div>
<div>
<em>Median</em>
</div>
<div>Random3<em>Median</em>
<div class="b">
<div class="c">Edit</div>
</div>
</div>
<div>Random4<em>Median</em>
</div>
</div>
In this case, how to get the two nodes contains 'Median' that doesn't have text before it using XPath?
I prefer not using the index because the node position could be random.
Maybe try:
//*[.='Median'][not(preceding-sibling::text()[normalize-space()])]
I have 4 other elements the same as below though they differ in title
<div id="r7695cf6b-f4a79" class="featureFoo ">
<div class="overview">
<h3 class="title">My Library</h3>
<p class="description">Some description</p>
</div>
<div class="foo">
<form>
<div id="ra41984bc58005" class="switch label-position-right non-required" aria-describedby="">
<input name="fooEnabled" type="checkbox" id="r14e560d0-bfbc-476a-a066-cbb3c043006c" checked="" class="">
<div class="foo-switch">
<div class="indicator"></div>
</div>
<span class="error" title=""></span>
</div>
</form>
</div>
</div>
I am trying to retrieve the nonhidden input element and to see if it is checked. I have this xpath
$x("//div[contains(#class, 'featureFoo')][./div/h3[text()='My Library']]//input[contains(#name,'fooEnabled')]")
which works in the Chrome console but it does not work in Ruby Cucumber's find() method
find(:xpath, "//div[contains(#class, 'featureFoo')][./div/h3[text()='My Library']]//input[contains(#name,'fooEnabled')]")
Is there more conversion that needs to be done to the xpath so it will work in cucumber's find() or could there be a better approach to this?
This one has me stumped., I'm trying to select the first class = csb-quantity-listbox object of the below using the XPATH //select[#class='csb-quantity-listbox'][1], but instead of selecting the first quantity listbox it's selecting ALL the listboxes on the page with that class (see image below).
What am I doing wrong?
<div class="gwt-product-detail-products-container">
<div class="gwt-product-detail-products-header-column">
</div>
<div id="gwt-product-detail-widget-id-12766" class="gwt-product-detail-widget">
<div class="gwt-product-detail-widget-image-column ui-draggable" title="12766">
<div class="gwt-product-detail-widget-options-column">
</div>
<div class="gwt-product-detail-widget-price-column">
</div>
<div class="gwt-product-detail-widget-quantity-panel">
<select class="csb-quantity-listbox" name="quantity_12766"></select>
</div>
<div class="gwt-bundle-add-to-cart-btn">
</div>
</div>
</div>
<div id="gwt-product-detail-widget-id-10617" class="gwt-product-detail-widget">
<div class="gwt-product-detail-widget-image-column ui-draggable" title="10617">
<div class="gwt-product-detail-widget-options-column">
</div>
<div class="gwt-product-detail-widget-price-column">
</div>
<div class="gwt-product-detail-widget-quantity-panel">
<select class="csb-quantity-listbox" name="quantity_10617"></select>
</div>
<div class="gwt-bundle-add-to-cart-btn">
</div>
</div>
</div>
</div>
Image:
You just need to put brackets around the statement before the [1]
Like so:
(//select[#class='csb-quantity-listbox'])[1]