gpt4 book ai didi

java - 如何使用循环在 Android 应用程序中按顺序选择项目

转载 作者:行者123 更新时间:2023-11-30 12:06:52 26 4
gpt4 key购买 nike

这里我有 3 张带有可点击元素的图片:

enter image description here

我使用这段代码,所以当它运行第一个循环时,它会选择FIRST 图像:

driver.findElement(By.id("com.offerup:id/circle")).click();

在我的代码运行完第二个循环后,我希望它返回并选择SECOND 图像,依此类推。

我已经从 uiautomatorviewer 获得了我的元素,可以在这里看到:

enter image description here

我不确定使用什么命令来解决我的问题,有人可以帮我吗?

这是我的完整代码,以便更好地检查:

public void SimpleTest() throws InterruptedException {


driver.findElement(By.id("com.offerup:id/email_button")).click();

By path = By.xpath("//*[@text='Enter your email address']");

driver.findElement(path).sendKeys("xxxxx@gmail.com");
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/next_button")).click();

By path1 = By.xpath("//*[@text='']");

driver.findElement(path1).sendKeys("xxxxx");

driver.findElement(By.id("com.offerup:id/main_text")).click();
Thread.sleep(10000);

By path2 = By.xpath("//*[@text='OfferUp']");

driver.findElement(path2).click();
Thread.sleep(10000);

driver.findElement(By.id("com.offerup:id/nav_post_item")).click();
Thread.sleep(5000);

driver.findElement(By.id("com.offerup:id/addPhotoFromGallery")).click();

try {

driver.findElement(By.id("com.simplemobiletools.gallery:id/dir_thumbnail")).click();

driver.findElement(By.id("com.simplemobiletools.gallery:id/medium_thumbnail")).click();
}

catch (Exception e) {

driver.findElement(By.id("com.offerup:id/circle")).click();

driver.findElement(By.id("com.offerup:id/done")).click();
}

By path3 = By.xpath("//*[@text='Name, brand, model, etc.']");

driver.findElement(path3).sendKeys("Iphone icloud unlocked");

driver.findElement(By.id("com.offerup:id/footer_button")).click();

driver.findElement(By.id("com.offerup:id/see_more")).click();

driver.findElement(By.id("com.offerup:id/category_list_row_text"));

By path4 = By.xpath("//*[@text='Cell Phones']");

driver.findElement(path4).click();

By path5 = By.xpath("//*[@text='Description']");

driver.findElement(path5).sendKeys("");
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/conditionSeekBar")).sendKeys(new CharSequence[] { " " });

driver.findElement(By.id("com.offerup:id/footer_button")).click();

By path6 = By.xpath("//*[@text='$0']");

driver.findElement(path6).sendKeys("200");
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/firmPrice")).click();

driver.findElement(By.id("com.offerup:id/footer_button")).click();

driver.findElement(By.id("com.offerup:id/edit")).click();

By path7 = By.xpath("//*[@text='Zip code']");

driver.findElement(path7).sendKeys("xxxx");
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/saveLocation")).click();

if(driver.findElement(By.id("com.offerup:id/shipping_checkbox")).isSelected()){
driver.findElement(By.id("com.offerup:id/shipping_checkbox")).click();
}

driver.findElement(By.id("com.offerup:id/footer_button")).click();
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/footer_button")).click();
Thread.sleep(4000);

driver.findElement(By.id("com.offerup:id/footer_button")).click();

driver.closeApp();
Thread.sleep(50000);

int index = 100;
do {
index --;

By path8 = By.xpath("//*[@text='OfferUp']");

driver.findElement(path8).click();
Thread.sleep(5000);

driver.findElement(By.id("com.offerup:id/nav_post_item")).click();
Thread.sleep(5000);

driver.findElement(By.id("com.offerup:id/addPhotoFromGallery")).click();

try {

driver.findElement(By.id("com.simplemobiletools.gallery:id/dir_thumbnail")).click();

driver.findElement(By.id("com.simplemobiletools.gallery:id/medium_thumbnail")).click();
}

catch (Exception e) {

driver.findElement(By.id("com.offerup:id/circle")).click();

driver.findElement(By.id("com.offerup:id/done")).click();
}

By path9 = By.xpath("//*[@text='Name, brand, model, etc.']");

driver.findElement(path9).sendKeys("Iphone XR Icloud unlocked"); // set ad name here

driver.findElement(By.id("com.offerup:id/footer_button")).click();

driver.findElement(By.id("com.offerup:id/see_more")).click();

driver.findElement(By.id("com.offerup:id/category_list_row_text"));

By path10 = By.xpath("//*[@text='Cell Phones']");

driver.findElement(path10).click();

By path11 = By.xpath("//*[@text='Description']");

driver.findElement(path11).sendKeys("");
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/conditionSeekBar")).sendKeys(new CharSequence[] { " " });

driver.findElement(By.id("com.offerup:id/footer_button")).click();

By path12 = By.xpath("//*[@text='$0']");

driver.findElement(path12).sendKeys("200");
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/firmPrice")).click();

driver.findElement(By.id("com.offerup:id/footer_button")).click();

driver.findElement(By.id("com.offerup:id/footer_button")).click();
Thread.sleep(2000);

driver.findElement(By.id("com.offerup:id/footer_button")).click();
Thread.sleep(4000);

driver.findElement(By.id("com.offerup:id/footer_button")).click();

driver.closeApp();
Thread.sleep(50000);

} while (index > 0);
index = index + 1;



}


}

最佳答案

因为您正在尝试点击网格布局中的图像。以下是实现该目标的步骤。

  1. 确定网格元素。
  2. 将行添加到列表中。
  3. 然后遍历每一行点击图像元素。

下面是帮助实现它的代码函数。

public void selectPhotos(int numberOfPhotosToSelect){

//Find the grid element
WebElement gridAlbum= driver.findElement(By.xpath("xpath of the full grid of album"));

//Add the rows of element to the list
List<WebElement> gridRows=gridAlbum.findElements(driver.findElement(By.xpath("xpath of the row element");

//find the number of rows
System.out.println("Number of Rows"+gridRows.size());

for(WebElement row:gridRows) {

List<WebElement> cells=row.findElements(driver.findElement(By.xpath("xpath of the Image");

for(WebElement cell:cells) {

if(numberOfPhotosToSelect>0) {
cell.click();
numberOfPhotosToSelect--;
}
else
break;

}

}
}

关于java - 如何使用循环在 Android 应用程序中按顺序选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55294344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com