假设我在 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();
我只想点击五个中的一个。 我将使用什么命令来实现这一点?
最佳答案
生成 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();
关于java - 如何通过索引选择随机元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308391/