Selenium:获取包含特定div类的div



我有一个页面结构,其中包含分配给多个div的类名。但是有一个特定的div会动态地包含一个特定的div.class

<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>

如果你在这里看到这个类ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM第4行将包含在一个特定的类中。虽然可能有多个ProductVariants__VariantCard-sc-1unev4j-3 bEuNss

如何获得包含ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYMProductVariants__VariantCard-sc-1unev4j-3 bEuNss

这是我尝试过的

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')]")

但是这里给出的是内部div而不是整个外部div

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

要识别具有class="ProductVariants__VariantCard-sc-1unev4j-3 bEuNss<div>和具有class="ProductVariants__RadioButtonInner-sc-1unev4j-6 fgFqYM<div>,您可以使用以下定位策略:

driver.find_element(by=By.XPATH, value="//div[starts-with(@class, 'ProductVariants__RadioButtonInner')]//ancestor::div[starts-with(@class, 'ProductVariants__VariantCard')]")

如果要选择包含特定后代节点的祖先节点请尝试

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')]]")

相关内容

  • 没有找到相关文章

最新更新