您的正确假设是,定位<span class="price-percent-reduction">
将返回多个元素。您似乎正在尝试在页面上找到特定产品,并且价格降低了。因此,您需要在所需产品的上下文中找到span
。
这是一个扩展的XPath,它查看产品标题并根据该价格获取折扣百分比:
//ul[@id='homefeatured']//div[h5/a[@title='Printed Chiffon Dress']]/div/span[@class='price-percent-reduction']
此选择器返回1个结果-20%
,这是产品Printed Chiffon Dress
的折扣百分比。
这可以概括为:
//ul[@id='homefeatured']//div[h5/a[@title='product title here']]/div/span[@class='price-percent-reduction']
XPath可以细分如下:
该网站有两个产品列表-“热门”和“最畅销”这两个产品列表具有重复的产品-一些“热门”产品也在最畅销列表中。为了检索单个产品元素,我们需要指定列表。上面的XPath指定由//ul[@id='homefeatured']
子句指示的'Popular'列表。请注意,您可以将其更改为//ul[@id='blockbestsellers']
,以从“最佳卖家”列表中进行选择。
在//ul[@id='homefeatured']
上选择之后,我们使用出现在子句title
中的//div[h5/a[@title='product title here']]
属性来查询列表中的产品。该查询告诉我们搜索包含标题div
的包含product title here
元素的路径。
一旦根据产品名称选择了产品,我们就可以查询价格降低的百分比,即条款的/div/span[@class='price-percent-reduction']
部分。
使用生成的XPath,您可以将product title here
更改为Printed Chiffon Dress
或任何其他项目的标题,并获得该产品下显示的%off(-5%)值。
值得注意的是-我在Google Chrome浏览器中使用了XPath帮助程序扩展,因此我可以实时测试大多数XPath,并且可以编写复杂的评估,例如我上面编写的路径。这就是我用来根据您提供的网站URL测试此XPath的机制。
,
要获取减少量-5%
的唯一元素,请使用以下xpath。
//ul[@id='homefeatured'][.//a[@title='Printed Summer Dress']]//div[@class='right-block']//span[@class='price-percent-reduction'][text()='-5%']
得出WebDriverWait
和visibilityOfElementLocated
()
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement itempricediscount=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@id='homefeatured'][.//a[@title='Printed Summer Dress']]//div[@class='right-block']//span[@class='price-percent-reduction'][text()='-5%']")));
System.out.println(itempricediscount.getText());
,
这是一个css:
#homefeatured > li:nth-child(5) .left-block .price-percent-reduction
我要做的是在chrome元素面板中搜索它,并一直保持下去,直到有独特之处
本文链接:https://www.f2er.com/3122184.html