【问题标题】:Appium + wd (nodejs) get sibling elementAppium + wd (nodejs) 获取兄弟元素
【发布时间】:2017-11-26 18:21:51
【问题描述】:

我在使用 wd 的 nodejs 上有一个 appium(android 黑盒测试)问题:

目前我无法获得兄弟元素。 Id 不是唯一的,架构始终相同 ->

ListView
  |
  |
  Frame
  | |
  | |
  | TextView[@text headline0]
  | |
  | | 
  | Button //no text - just an icon
  |
  Frame
  | |
  | |
  | TextView[@text headline1]
  | |
  | | 
  | Button //no text - just an icon
  |
  Frame
  | |
  ...

我的问题是,我想单击例如旁边的按钮。标题 9.我写了一个scrollToElement(title) 函数,只要“headline9”可见,它就会滚动..

但是现在我不能使用像 [//Frame[@index="9"]/] 这样的 xpath,因为在当前可见区域中索引似乎又从 0 开始......所以也许 @index="9" 变成了 @index=2 或者有时是 @index=1。 .

现在我的想法是使用以下方式选择按钮:

  .elementByXPath("//android.widget.TextView[@text='headline9']/../android.widget.Button")

但是好像不行(找不到元素)

还有其他人有想法吗?

非常感谢!

【问题讨论】:

    标签: android node.js appium


    【解决方案1】:

    尝试通过elementsByXPath() 获取所有TextView,然后以相同方式获取所有按钮。如果 TextViews Text 等于您的“headline9”,则返回 Button,否则遍历找到的 TextViews。如果在可见区域找不到它,请滚动并再次搜索。

    new Promise(function (resolve, reject) {
     (function yourfunctionId(id, context){
      elementsByXPath(Frame[@index=id]TextView){
       elementsByXPath(Frame[@index=id]Button){
        if(TextView[0] === 'undefined'){
         //scroll in ListView
         //call your function recursively from begin (id =0)
         yourfunction(0)
        }
        if(TextView[0].getAttribute('name') == 'headline9'){
         return Button[0]
        }
        else{
         //next
         //call your function recursively
         yourfunction(id++)
        }
       }
      }
    )}
    

    要么滚动查看 von Frame-Element 的大小,要么您可能需要检查第一个 Frame 是否部分可见。

    【讨论】:

      【解决方案2】:

      希望对你有帮助

      var elemList = yield driver.elementsByClassName('android.support.v7.widget.LinearLayoutCompat'); //get List of elements 
      for(let elem of elemList) {
          //traverse list of elements
          let text = yield elem.elementById('subElementId').text(); //find subElement
          //Your code logic
       }
      

      获取元素列表,然后找到子元素。我正在使用“yiewd”库,因为“wd”的概念是相同的。 谢谢!

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-23
        • 2022-01-02
        • 2017-08-25
        • 1970-01-01
        相关资源
        最近更新 更多