java - 如何按数字顺序处理多个索引
问题描述
我正在为 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();
}
}
在它向我的消息中的第一个人发送消息后,我希望它返回并为第二个人、第三个人做同样的事情,依此类推。有谁知道我可以使用什么命令?
解决方案
您可以使用 driver.findElements(By) 获取所有元素的列表并遍历它们。你只需要调整你的xpath,让它选择所有人,而不仅仅是一个人。在循环中,您发送消息并返回。
推荐阅读
- java - 正则表达式匹配三个非连续数字,其中数字需要为 0 或 1
- reactjs - 材质 UI ListItemSecondaryAction 在 ListItem 中垂直对齐
- python - 如何在给定包含字典的列表列表的情况下找到交集
- olap - 从 CSV 文件创建多个 Pinot 段(Apache Pinot 数据摄取)
- laravel - 我正在尝试从不存在的数据库中删除我的记录。并更新我存在的记录。使用 laravel
- vuejs2 - VueJs 组件的 Prop 相关计算属性对对象数组 Prop 更改没有反应
- reactjs - 模拟更改输入类型 = 文件后不调用我的模拟函数
- python - 如何向熊猫数据透视表添加新索引?
- elasticsearch - 如何使用自定义转换 Spring-data-elasticsearch
- c# - 将连接字符串传递给.net-core 3 MVC中的其他项目