I am new to XPath and confused. Anyone can have a quick glance and see what is wrong in my syntax?
I'm trying to select all direct child div's
of id="list-overview"
which has two child nodes somewhere down their tree containing data-price<=20
and a div containing "Orange" text
let xy = $x(`//*[@id="list-overview"]/div[./div/a/div/div/div[@data-price<=20]][./div/a/div/div[@class='fruit'][contains(.,'Orange')]])`)
to break it up. I have tested these two separately and they worked.
`//*[@id="list-overview"]/div[./div/a/div/div/div[@data-price<=20]]`
`//*[@id="list-overview"]/div[./div/a/div/div[@class='fruit'][contains(.,'Orange')])]`
I just can't seem to combine them somehow and not sure what I'm doing wrong?
EDIT:
Tried the suggestions and the following xpath doesn't throw an exception anymore. But it returns empty Array while there are elements matching price < 20 and fruit="Orange"
$x(`//*[@id="list-overview"]/div[./div/a/div/div/div[@data-price<=20] and ./div/div/a/div/div[@class='fruit'][contains(.,'Orange')]]`)
CodePudding user response:
If I understand your idea, you are missing the logical and
between these 2 conditions. I corrected the XPath expression accordingly. Please try this:
let xy = $x(`//*[@id="list-overview"]/div[./div/a/div/div/div[@data-price<=20] and ./div/a/div/div[@class='fruit'][contains(.,'Orange')]])`)
UPD
Please try this:
let xy = $x(`//*[@id="list-overview"]/div[.//div[@data-price<=20] and .//div[@class='fruit'][contains(.,'Orange')]])`)
CodePudding user response:
Instead of this
$x(//*[@id="list-overview"]/div[./div/a/div/div/div[@data-price<=20]][./div/a/div/div[@class='fruit'][contains(.,'Orange')]]))
You could do this to combine both of them,
to tightly couple both the child
$x(//*[@id="list-overview"]/div[./descendant::div[@data-price<=20]] and [./div/a/div/div[@class='fruit'][contains(.,'Orange')]])
or to either have one of them and still you would want to proceed further.
$x(//*[@id="list-overview"]/div[./descendant::div[@data-price<=20]] or [./div/a/div/div[@class='fruit'][contains(.,'Orange')]])
Also, I have included descendant
to make it more readable.