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

Selenium : Get a div that contains a specific div class inside it

I have a page structure which contains a classname assigned to multiple divs. But there is one specific div that will contain a specific div.class inside it which happens dynamically.

<div class="ProductVariants__VariantCard-sc-1unev4j-3 bEuNss">
   <div class="ProductVariants__RadioButtonContainer-sc-1unev4j-4 jqJIBg">
     <div class="ProductVariants__RadioButtonOuter-sc-1unev4j-5 jcgpFW">
       <div class="ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM">
       </div>
     </div>
   </div>
   <div class="ProductVariants__VariantDetailsContainer-sc-1unev4j-7 fvkqJd">
     <p class="ProductVariants__VariantUnitText-sc-1unev4j-8 bZvIga">50 g</p>
     <div class="ProductVariants__PriceContainer-sc-1unev4j-9 jjiIua">
       ₹111 
       <span class="ProductVariants__MRPText-sc-1unev4j-10 jEinXG">
         ₹345
       </span>
     </div>
   </div>
</div>

If you see here this class ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM Line number 4 will be contained within one that specific class. Though there could be multiple of ProductVariants__VariantCard-sc-1unev4j-3 bEuNss.

How do I get the class ProductVariants__VariantCard-sc-1unev4j-3 bEuNss which contains ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM inside it.

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

This is what I have tried

driver.find_element(by=By.XPATH, value="//div[contains(@class,'ProductVariants__VariantCard-sc-1unev4j-3 bEuNss')]//div[contains(@class, 'ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM')]")

But this instead gives me the inner div and not the whole outer div

'<div class="ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM"></div>'

>Solution :

If you want to select ancestor node that contain specific descendant node try

driver.find_element(by=By.XPATH, value="//div[contains(@class,'ProductVariants__VariantCard-sc-1unev4j-3 bEuNss') and .//div[contains(@class, 'ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM')]]")
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