【问题标题】:How to handle multiple indexes in numerical order如何按数字顺序处理多个索引
【发布时间】:2019-07-18 05:38:24
【问题描述】:

我正在为 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();
}


}

在它向我的消息中的第一个人发送消息后,我希望它返回并为第二个人、第三个人等执行相同的操作。有谁知道我可以使用什么命令?

【问题讨论】:

    标签: java android selenium automation appium


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以像这样在 webElement 列表中找到所有关注者

      List list1=d.findElements(By.xpath("//android.widget.LinearLayout[@index]")); //这将为您提供您的关注者列表。

      现在你可以像这样迭代它

      List list1=d.findElements(By.xpath("//android.widget.LinearLayout[@index]"));
          for(WebElment list1:ele){
      
          driver.findElement(ele).click(); 
          By path4 = By.xpath("//*[@text='Message…']");
          driver.findElement(path4).sendKeys("Hello");
          driver.navigate().back()
      
          }
      

      希望对您有所帮助!

      【讨论】:

      • 列出 list1=d.findElements(By.xpath("//android.widget.LinearLayout[@index]"));在这一行中,"d" 带有红色下划线。此外,在这一行 driver.findElement(ele).click(); “ele”带有红色下划线。
      • d 将成为您的司机
      猜你喜欢
      • 2019-07-18
      • 2012-12-26
      • 2013-09-10
      • 2021-11-04
      • 2017-02-03
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多