【问题标题】:I need a more efficient way to handle multiple indexes in numerical order我需要一种更有效的方法来按数字顺序处理多个索引
【发布时间】:2019-07-18 07:25:41
【问题描述】:

我正在尝试制作一个可以自动向我的 DM 中的人发送消息的机器人。我让它给第一个人发送消息,返回,然后使用这里的代码给第二个人发送消息。

By path3 = By.xpath("//android.widget.LinearLayout[@index='1']"); 
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();
Thread.sleep(5000);
driver.findElement(By.id("com.instagram.android:id/action_bar_button_back")).click();

By path5 = By.xpath("//android.widget.LinearLayout[@index='2']"); 
driver.findElement(path5).click();

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

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

但这并不高效,因为如果我使用这种方法,我必须为每个新索引创建一个新行。

有人知道如何将它改写成更有效的风格吗?

【问题讨论】:

    标签: java android selenium automation appium


    【解决方案1】:

    您可以使用 for 循环和实际的联系人数量。这里我假设它是一个 range()

    contacts = range(10)
    for x in contacts:
        By path3 = By.xpath("//android.widget.LinearLayout[@index='{0}']".format(x);
        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();
        Thread.sleep(5000);
        driver.findElement(By.id("com.instagram.android:id/action_bar_button_back")).click();
    

    【讨论】:

      【解决方案2】:

      请试试这个:

      public void sendMsg() {
      
          List<MobileElement> paths = (List<MobileElement>) driver.findElements(By.xpath("//*[@class='android.widget.LinearLayout']"));
          WebDriverWait wait = new WebDriverWait(driver, 30);
      
            for (MobileElement path : paths) 
             {
              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();
               wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("com.instagram.android:id/action_bar_button_back"))));
               driver.findElement(By.id("com.instagram.android:id/action_bar_button_back")).click();
      
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-18
        • 2011-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        相关资源
        最近更新 更多