【问题标题】:How to check if text exist inside class using Selenium and JavaScript如何使用 Selenium 和 JavaScript 检查类中是否存在文本
【发布时间】:2020-11-16 01:13:33
【问题描述】:

我是 Selenium 的新手。我喜欢验证特定类是否包含 JavaScript 中的文本 sn-p。

我有以下 HTML:

<div class="product-small">
   <div class="data">
      <p>This is some Text</p>
   </div>
</div>
<div class="product-small">
   <div class="data">
      <p>This is some Text</p>
   </div>
</div>
...

上面的类在页面上可以多次使用,所以我必须迭代“product-small”类来检查所有 12 个类是否都包含文本“some”。如果该“product-small”类的 12 个元素之一不包含文本“some”,则测试将失败。

我是这样想的:

const cars = driver.findElements(By.className('product-small'));
    cars.forEach(function (element) {
        // Check if element contains "some" inside class "data"
        // If element does not contain "some", test will fail
    });

谢谢你:)

【问题讨论】:

    标签: javascript selenium selenium-webdriver xpath webdriver


    【解决方案1】:

    一般的解决方案是使用以下基于Locator Strategy 收集元素并探测长度,如果它等于12,则将其标记为通过 否则将其标记为失败

    const cars = driver.findElements(By.xpath("div[@class='product-small']//p[contains(., 'some')]"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 2012-07-12
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多