【问题标题】:How to select a random element by its index?如何通过索引选择随机元素?
【发布时间】:2019-08-13 23:00:02
【问题描述】:

假设我在一个 android 应用中有五个元素,它们都有各自的索引

driver.findElement(By.xpath("//android.widget.FrameLayout[@index='0']/android.widget.ImageView[@index='0']")).click();
driver.findElement(By.xpath("//android.widget.FrameLayout[@index='1']/android.widget.ImageView[@index='0']")).click();
driver.findElement(By.xpath("//android.widget.FrameLayout[@index='2']/android.widget.ImageView[@index='0']")).click();
driver.findElement(By.xpath("//android.widget.FrameLayout[@index='3']/android.widget.ImageView[@index='0']")).click();
driver.findElement(By.xpath("//android.widget.FrameLayout[@index='4']/android.widget.ImageView[@index='0']")).click();

我只想点击五个中的一个。 我会使用什么命令来实现这一点?

【问题讨论】:

    标签: java android selenium automation appium


    【解决方案1】:

    生成一个介于 0 和 4 之间的随机数

    Random r = new Random();
    int result = r.nextInt(5);
    
    
    
    driver.findElement(By.xpath("//android.widget.FrameLayout[@index='"+result+"']/android.widget.ImageView[@index='0']")).click();
    

    【讨论】:

      【解决方案2】:

      生成一个随机数

      Random rand = new Random(); 
      
      int randomNumber = random.nextInt(max - min + 1) + min;
      

      然后将此 randomNumber 分配给索引。

      【讨论】:

        猜你喜欢
        • 2013-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        • 1970-01-01
        • 2010-12-18
        • 2021-12-30
        相关资源
        最近更新 更多