gpt4 book ai didi

java - 获取页面中多次出现的div的xpath

转载 作者:行者123 更新时间:2023-11-28 06:52:56 26 4
gpt4 key购买 nike

我有一个奇怪的要求。我有一个名为“expandThisSection”的 div 类。而我有

<div id=​"requiredAccessoriesContentPlaceHolder" class=​"expandThisSection">​No required accessories are available for this product.​</div>​

<div id=​"requiredAccessoriesContentPlaceHolderMeasurement" class=​"expandThisSection">​&nbsp;​</div>​

<div id=​"optionalAccessoriesContentPlaceHolder" class=​"expandThisSection">​No optional accessories are available for this product.​</div>​

<div id=​"optionalAccessoriesContentPlaceHolderMeasurement" class=​"expandThisSection">​&nbsp;​</div>​

<div class=​"expandThisSection">​
<div style=​"width:​95%">​mytext</div>​
​<ul class=​"movePos">​…​</ul>​
<div><b>test</b>​</div>
​<div>​<b>abc</b> Get this text</div>
​<div id=​"vendLogo">​…​</div>
</div>

<div class="expandThisSection">
<table>...</table>
</div>

我想要具有 95% 宽度样式的 div 的内容。我想要的值是“mytext”。但是我无法找到相同的 xpath。

此外,我还需要 xpath 来查找位于 id= “vendLogo”的 div 上方的 div 内容。那就是我想要“获取此文本”。

注意:我保证这个粗体标签将包含“abc”

怎么做?我在 Java 中使用 Selenium

得到第一个。无法获得第二个。

代码:

List<WebElement> currentSkuDescription = driver.findElements(By.xpath("//div[@class='expandThisSection']/div"));
for(WebElement currentItem: currentSkuDescription) {
WebElement descriptionBlock = currentItem.findElement(By.xpath("//div[contains(@style, 'width:95%')]"));
description= descriptionBlock.getText();
}

最佳答案

尝试删除 @class='expandThisSection' 因为你想要的 div 没有那个类属性(它的父级有)。此外,完全匹配也是可能的。

By.xpath("//div[@style='width:95%']
By.xpath("//div[contains(@style, 'width:95%')]

这些都应该有效。如果您想确保相对于父对象抓取正确的对象,请使用 XPath 轴...

//div[contains(@class, 'expandThisSection')]/child::div[contains(@style, 'width:95%')]

关于java - 获取页面中多次出现的div的xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33675621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com