【问题标题】:How to find ng-repeater that contains a specific text with selenium and Python如何使用 selenium 和 Python 查找包含特定文本的 ng-repeater
【发布时间】:2015-12-27 17:58:04
【问题描述】:

我有一个 Angular Web 应用程序,我想用 selenium 和 python 进行测试 html文件是这样的:

<div ng-repeat="item in items">
    <div ng-repeat="address in addresses">
       bla
    </div>
    <div ng-repeat="address in addresses">
       dodo
    </div>
</div>
<div ng-repeat="item in items">
    <div ng-repeat="address in addresses">
       foo
    </div>
    <div ng-repeat="address in addresses">
       didi
    </div>

</div>

使用 selenium 和 python,我想在项目中继器中查找项目中的所有元素,但前提是包含特定文本(例如 bla)

这意味着我想通过网页上显示的文本获取第一个转发器中的所有项目,而不是第二个转发器中的所有项目

我尝试过类似的方法

browser.find_element_by_xpath("//div[ng-repeat='item in items'] and (contains(text(), 'bla'))")

我的错误:

invalidSelectorException

【问题讨论】:

    标签: python angularjs selenium


    【解决方案1】:

    代替:

    browser.find_element_by_xpath("//div[ng-repeat='item in items'] and (contains(text(), 'bla'))")
    

    尝试:

    browser.find_elements_by_xpath("//div[@ng-repeat='item in items' and contains(., 'bla')]")
    

    我在那里所做的是更改右方括号的位置并在 ng-repeat 的开头添加一个@ - 用于属性检查的语法,将find_element_by_xpath更改为find_elements_by_xpath,最后更改了text().(一个点)。当您在不同的子节点中有多个文本时,这通常会更好。

    【讨论】:

      【解决方案2】:

      我会在 xpath 下尝试-

      //div[@ng-repeat='item in items']/div[@ng-repeat='address in addresses' and contains (text(),'bla')]
      

      对于html-

      <div>
      <div ng-repeat="item in items">
          <div ng-repeat="address in addresses">
             bla
          </div>
          <div ng-repeat="address in addresses">
             dodo
          </div>
      </div>
      <div ng-repeat="item in items">
          <div ng-repeat="address in addresses">
             foo
          </div>
          <div ng-repeat="address in addresses">
             didi
          </div>
      
      </div>
      </div>
      

      HERE 上查看演示测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-05
        • 1970-01-01
        • 1970-01-01
        • 2012-09-01
        • 2021-06-11
        • 1970-01-01
        • 2016-12-10
        • 2022-01-22
        相关资源
        最近更新 更多