1

So I have some complicated situation in the DOM where I need to create XPath and get its child element. I have the following XPath

((//p[contains(text(),'" + read_contract_title() + "')]/parent::div/parent::div)[2]//button)[2]

The above is basically a ... more options button, that I need to click and then click the child element inside of it.

How do I get about getting the child element (button) inside the above XPath?

Looking forward to your help and reply.

DZR
  • 195
  • 1
  • 2
  • 12
  • 3
    Can you provide a simple example of the HTML you'll be querying? Posting an xpath with no context isn't really helpful. – Kyle Mar 22 '22 at 14:25

1 Answers1

1

In case parent element can be located with this XPath

"((//p[contains(text(),'" + read_contract_title() + "')]/parent::div/parent::div)[2]//button)[2]"

The button element inside it could be simply located with

"((//p[contains(text(),'" + read_contract_title() + "')]/parent::div/parent::div)[2]//button)[2]//button"

In case this is the only button element inside that parent element

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • this is good answer. It helped me reach the solution. so I have to go had few more parents and then get the child element. Will accept an answer in 5 mins once SO allows it :) thank you @prophet – DZR Mar 22 '22 at 14:29
  • 1
    You should warn OP about the [perils of using `contains(text(),"substring")` and the better solutions](https://stackoverflow.com/a/71255563/290085). Also, advise OP to post the subject markup so that we can explain such issues (and probably suggest a cleaner XPath too) in concrete terms. – kjhughes Mar 22 '22 at 14:40
  • @kjhughes sure, but since he did not provide more details I could base my answer only on details OP shared in the question – Prophet Mar 22 '22 at 14:45
  • OP appears to be happy, but it won't last if the targeted text shifts out of position one. Future readers deserve the warning too anytime the problematic `contains(text(), "substring")` construct appears in a question (or an answer). – kjhughes Mar 22 '22 at 14:49
  • Again, you are right @kjhughes, but since this question is about `selenium` and `xpath` only XPath 1.0 is relevant here, so there is no really need to worry about issues with Xpath 2.0+ in this case. – Prophet Mar 22 '22 at 15:01
  • You may be missing the more import part of the point. The `contains(text(), "substring")` construct is more dangerous under XPath 1.0 than 2.0 because under 1.0 it ***silently only considers the first text node***; at least under XPath 2.0 it's an impossible-to-miss error. See [my full answer](https://stackoverflow.com/a/71255563/290085) for further explanation and details. – kjhughes Mar 22 '22 at 15:12
  • I read it today (again). Maybe I just did not understood the entire deepness of that. My XPath knowledge is really much much less than yours since my actual XPath experience is limited with locating elements on a web page for Selenium uses only :) I hope your answers (including the one you linked to here) and notes will be useful to as many people as possible. – Prophet Mar 22 '22 at 15:23