Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to distinguish 2 button base on their parent/ancestor element in Selenium (PYTHON)?

<a href="/google.com">
<div> ABC </div>
<span>
<button class="btn"> Show more </button>
<span/>
</a>

<div>
<div>ABC</div>
<span>
<button class="btn"> Show more </button>
<span/>
</div>

As you guys see here, we have 2 BUTTONS and I need to click to BUTTON whose ancestor is not <a/> tag, because if I click to button whose ancestor is tag, it will redirect me to other pages. So I don’t want this behaviour.

The obvious solution is I can use absolute Xpath for this, but it’s not a good way because HTML DOM or css structure could be changed so it’
s not stable.

So how can I distinguish 2 button, using ancestor or something related? I mean other better ways.
Thank you guys for helping me so much!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To select a button that has no a tag ancestor can be done with the following XPath:

//button[not(ancestor::a) and(contains(.,'Show more'))]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading