【问题标题】:Locating second, third, fourth,...eigth element in protractor using css locator使用 css 定位器在量角器中定位第二个、第三个、第四个、...第八个元素
【发布时间】:2017-02-08 04:46:48
【问题描述】:

我一直在使用量角器进行测试,除了通过 css 之外无法引用元素,因为它只具有给定的类属性。问题是有超过 7 个元素具有此类名称。因此我使用语法

element.all(by.css('h4.ng-binding')).first(); 

对于第一个,它工作正常,但对于其他它不起作用!我使用与第一个相同的逻辑。这是我的 sn-ps 代码,供其他人找到它们。

  element.all(by.css('h4.ng-binding')).second();
  element.all(by.css('h4.ng-binding')).third();
  element.all(by.css('h4.ng-binding')).fourth();
  element.all(by.css('h4.ng-binding')).fifth();
  element.all(by.css('h4.ng-binding')).sixth();
  element.all(by.css('h4.ng-binding')).seventh();
  element.all(by.css('h4.ng-binding')).eighth();

【问题讨论】:

    标签: javascript angularjs selenium jasmine protractor


    【解决方案1】:

    量角器中没有这些功能。只有first()last()get() 是可用的函数。您可以在 ElementArrayFinder 之上使用这 3 个函数来实现一切。方法如下-

    element.all(by.css('h4.ng-binding')).first(); //get the first element
    element.all(by.css('h4.ng-binding')).get(0); //get the first element as get() function is a '0' based index
    element.all(by.css('h4.ng-binding')).get(1); //get the second element
    element.all(by.css('h4.ng-binding')).get(2); //get the third element
    element.all(by.css('h4.ng-binding')).last(); //get the last element
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      现在你可以使用.second(); .third(); .fourth(); 等函数了 - 我开发了一个小包 - https://github.com/Marketionist/protractor-numerator - 可以与量角器一起使用来选择第二个、第三个、第四个等元素。

      【讨论】:

        【解决方案3】:

        使用 nth-child 即

        element.all(by.css('h4.ng-binding:nth-child(2)'))...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-06-02
          • 1970-01-01
          • 2017-03-11
          • 2023-03-20
          • 2019-06-18
          • 2019-09-16
          • 1970-01-01
          相关资源
          最近更新 更多