【问题标题】:Protractor find a certain value inside td class量角器在 td 类中找到某个值
【发布时间】:2017-11-18 12:36:18
【问题描述】:


我一直在研究如何迭代表的许多不同示例。

如果我将下面的代码用于 selenium,我会得到我想要的。

driver.findElement(By.xpath("//td[contains(.,'Apple Pomace')]"));

虽然我想改为迭代代码并找到以下值:

<td class="ng-binding">Apple Pomace</td>

注意两个地方有苹果渣

See image

这是我的桌子:

<div class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1">
    <h3 ng-show="searchQuery" class="page-header page-header-sm"><span translate="TITLE_SEARCH_RESULTS" class="ng-scope">Search Results</span> <span class="label label-default ng-binding" ng-bind-html="searchQuery">Apple Pomace</span></h3>
    <h3 ng-show="!searchQuery" class="page-header page-header-sm ng-scope ng-hide" translate="TITLE_ALL_PRODUCTS">All Products</h3>

    <div class="alert-info ng-hide" ng-show="confirmation">
        <p class="ng-binding"></p>
    </div>

    <table class="table table-striped table-bordered table-condensed">
        <tbody><tr>
            <th translate="LABEL_IMAGE" class="ng-scope">Image</th>
            <th translate="LABEL_PRODUCT" class="ng-scope">Product</th>
            <th translate="LABEL_DESCRIPTION" class="ng-scope">Description</th>
            <th translate="LABEL_PRICE" class="ng-scope">Price</th>
            <th></th>
        </tr>
        <!-- ngRepeat: product in products --><tr data-ng-repeat="product in products" class="ng-scope" style="">
            <td><img src="/public/images/products/apple_pressings.jpg" class="img-responsive img-thumbnail" style="width: 200px" ng-click="showDetail(product.id)"></td>
            <td class="ng-binding">Apple Pomace</td> //here is the item I want to fetch  'Apple Pomace'
            <td><div ng-bind-html="product.description" class="ng-binding">Finest pressings of apples. Allergy disclaimer: Might contain traces of worms. Can be <a href="/#reccyle">sent back to us</a> for recycling.</div></td>
            <td class="ng-binding">0.89</td>
            <td>
                <div class="btn-group">
                    <a class="btn btn-default btn-xs" ng-click="showDetail(product.id)"><i class="fa fa-eye"></i></a>
                    <a class="btn btn-default btn-xs ng-hide" ng-click="addToBasket(product.id)" ng-show="isLoggedIn()"><i class="fa fa-cart-plus"></i></a>
                </div>
            </td>
        </tr><!-- end ngRepeat: product in products -->
    </tbody></table>

</div>

我已经编写了以下代码,但我认为它不正确。

 element.all(by.css('.ng-binding')).each(function(element, index)
             {
             element.getText().then(function (text)
             {


             });
             })

请注意,我在两个地方得到了“Apple Pomace”,这就是迭代 td 类“ng-binding”很重要的原因。

谁能帮帮我

提前谢谢你

【问题讨论】:

    标签: javascript loops protractor iterated-function


    【解决方案1】:

    如果你只想访问 td,你可以使用这个:

    element(by.cssContainingText('.table td', 'Apple Pomace'))
    

    如果您想按产品过滤表格中的行并获取元素,您可以使用下面的代码 sn-p:

    element.all(by.repeater('product in products')).filter(function(elem, index) {
      return elem.element(by.cssContainingText('td', 'Apple Pomace')).isPresent();
    }).first();
    

    【讨论】:

    • 当我尝试最后一个代码 sn-p 我得到元素不是函数。
    • 那是因为by.repeater() 是为element.all() 制作的...以element.all(by.repeater(... 开头,它会起作用。
    • 是的,我的错误我已经更新了代码.. 请立即尝试
    • 感谢现在效果更好,我如何添加期望验证:element.all(by.repeater('product in products')).filter(function(elem, index) { return elem .element(by.cssContainingText('td', 'Apple Pomace')).isPresent(); }).first();提前谢谢你
    • 只需在上面的元素上使用 isDisplayed() 并断言它为真。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2015-01-31
    • 1970-01-01
    • 2022-11-06
    • 2013-10-23
    相关资源
    最近更新 更多