【发布时间】:2018-09-27 10:29:16
【问题描述】:
我正面临抓取元素/元素的问题。我得到了具有“金额”值的产品列表,在该字段之间有两个按钮:“-”和“+”,这增加了该字段的总金额。
我需要抓住两者:加号或减号才能正确测试它。我已经通过 css 选择器完成了,但是现在需要通过 XPath 来完成。
var increaseIngredientButton = driver.FindElement(By.CssSelector(".product-ingredient-list > div:nth-child(2) > div:nth-child(3) > div:nth-child(3)"));
这里是 HTML sn-p:Here
首先,我需要找到合适的行 - product-ingredient-row(具体产品有很多行),通过它的名称区分它 - product ingredients-name (名称是唯一的),最后选择 div 加减号。有人可以告诉我应该怎么做,因为不幸的是不知道......
此外,使用 XPath 或 Cssselectors 更好吗?有没有可能用 XPath 稍微抓住一切?
<div class="product-details-container">
<div class="product-details-header">
<div class="product-details-name">Berlusconi</div>
<div class="product-details-price">
</div>
<div class="product-details-total-price">
<div>Total: </div>
<div>
<!-- react-text: 3872 -->11.90<!-- /react-text --><!-- react-text: 3873 --> €<!-- /react-text -->
</div>
</div>
<div class="product-details-close"></span></div>
</div>
<div class="product-details-scrollable-area">
<div class="product-details-allergenics">
<div>Allergenics:</div>
<div>None</div>
</div>
<div class="product-details-description-expander">
<div>
<div style="padding-bottom: 0px;">
<div style="padding: 16px; font-weight: 500; box-sizing: border-box; position: relative; white-space: nowrap; cursor: pointer;">
<div style=><span style="></span></div>
<button tabindex="0" type="button" style=>
<div>
<svg viewBox="0 0 24 24" style=">
<path d="M7.41 7.84L12 12.42l4.59-4.58L18 9.25l-6 6-6-6z"></path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="product-ingredient-row header">
<div class="product-ingredient-name">Ingredients:</div>
<div class="product-ingredient-price">Unit price:</div>
<div class="product-ingredient-quantity-container">Amount:</div>
</div>
<div class="product-ingredient-list">
<div class="product-ingredient-row">
<div class="product-ingredient-name">Kotipizza-juusto</div>
<div class="product-ingredient-price">
<!-- react-text: 3908 -->0.00<!-- /react-text --><!-- react-text: 3909 --> €<!-- /react-text -->
</div>
<div class="product-ingredient-quantity-container">
<div>−</div>
<div>1</div>
<div>+</div>
</div>
</div>
<div class="product-ingredient-row">
<div class="product-ingredient-name">tomaattikastike</div>
<div class="product-ingredient-price">
<!-- react-text: 3926 -->0.00<!-- /react-text --><!-- react-text: 3927 --> €<!-- /react-text -->
</div>
<div class="product-ingredient-quantity-container">
**<div>−</div>
<div>1</div>
<div>+</div>**
</div>
</div>
<div class="product-ingredient-row">
<div class="product-ingredient-name">kantarelli</div>
<div class="product-ingredient-price">
<!-- react-text: 3962 -->0.00<!-- /react-text --><!-- react-text: 3963 --> €<!-- /react-text -->
</div>
<div class="product-ingredient-quantity-container">
<div>−</div>
<div>1</div>
<div>+</div>
</div>
</div>
<div class="product-ingredient-row">
<div class="product-ingredient-name">savuporo</div>
<div class="product-ingredient-price">
<!-- react-text: 3971 -->0.00<!-- /react-text --><!-- react-text: 3972 --> €<!-- /react-text -->
</div>
<div class="product-ingredient-quantity-container">
<div>−</div>
<div>1</div>
<div>+</div>
</div>
</div>
<div class="product-ingredient-row">
<div class="product-ingredient-name">punasipuli</div>
<div class="product-ingredient-price">
<!-- react-text: 3980 -->0.00<!-- /react-text --><!-- react-text: 3981 --> €<!-- /react-text -->
</div>
<div class="product-ingredient-quantity-container">
<div>−</div>
<div>1</div>
<div>+</div>
</div>
</div>
<div class="product-ingredient-row">
<div class="product-ingredient-name">pizzapohja, runsaskuituinen</div>
<div class="product-ingredient-price">
<!-- react-text: 3989 -->0.00<!-- /react-text --><!-- react-text: 3990 --> €<!-- /react-text -->
</div>
<div class="product-ingredient-quantity-container">
<div>−</div>
<div>1</div>
<div>+</div>
</div>
</div>
</div>
<button class="add-ingredient-btn" tabindex="0" type="button" style=>
<div><span style=>Add Ingredient</span></div>
</button>
</div>
<div class="product-details-actions">
<div style=>
<button tabindex="0" type="button" style=>
<div>
<div style=>Add to cart</span></div>
</div>
</button>
</div>
</div>
</div>
【问题讨论】:
-
请阅读为什么screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
-
问题是我不知道 HTML 的哪个部分是相关的或“足够的”,在我看来,“仅”是我在屏幕截图中包含的部分。我需要将整个 sn-p 重写为文本吗? (这里是第一次发帖,如果有什么不应该的地方,请见谅)
-
就像打开 HTML 一样,复制 HTML 并将其粘贴到问题区域中。您是初学者,我们的一些志愿者可能会帮助您缩进 HTML,否则很难提供解决方案。
-
完成,我已删除样式信息以减少字母数量
-
太棒了,现在您想增加/减少哪个
product-ingredient-name的数量?即+或-
标签: c# selenium selenium-webdriver selenium-chromedriver