首页 > 解决方案 > 如何按数字顺序处理多个索引

问题描述

我正在为 Instagram 制作一个机器人,它会自动向我的关注者发送消息。我希望它向每个人发送消息,但我不确定如何做到这一点。

这是我的代码

@Test
public void SimpleTest() throws InterruptedException { //

driver.findElement(By.id("com.instagram.android:id/log_in_button")).click();


By path = By.xpath("//*[@text='Phone number, email or username']");
driver.findElement(path).sendKeys("draco_boys");
Thread.sleep(5000);

driver.findElement(By.id("com.instagram.android:id/password")).sendKeys("xxxxxxxx"); Thread.sleep(5000);

driver.findElement(By.id("com.instagram.android:id/next_button")).click();
Thread.sleep(5000);

By path2 = By.xpath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ImageView[@index='3']"); 
Thread.sleep(7000);
driver.findElement(path2).click(); 
Thread.sleep(5000);

By path3 = By.xpath("//android.widget.LinearLayout[@index='1']"); //first person in my messages
driver.findElement(path3).click(); 

By path4 = By.xpath("//*[@text='Message…']");
driver.findElement(path4).sendKeys("Hello");

driver.findElement(By.id("com.instagram.android:id/row_thread_composer_button_send")).click();

driver.findElement(By.id("com.instagram.android:id/action_bar_button_back")).click();
}


}

在它向我的消息中的第一个人发送消息后,我希望它返回并为第二个人、第三个人做同样的事情,依此类推。有谁知道我可以使用什么命令?

标签: javaandroidseleniumautomationappium

解决方案


您可以使用 driver.findElements(By) 获取所有元素的列表并遍历它们。你只需要调整你的xpath,让它选择所有人,而不仅仅是一个人。在循环中,您发送消息并返回。


推荐阅读