我是使用Java的Selenium Webdriver的新手。我的<a class="row-title">
属性最多可包含21个web元素。我想循环执行代码,以便可以将Java硒代码合并为较小的代码,目前,我必须为所有21个测试编写相同的脚本。例如。
<a class="row-title">1</a>>
........
........
<a class="row-tilte">21</a>.
also i have to do same clicking and update procedure up to 1 to 21
package Dev_admin;
import java.nio.channels.SelectableChannel;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
public class present extends login{
@Test(priority = 1)
public void update1() {
driver.findElement(By.xpath(".//*[@id='post-1217']/td[1]/strong/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
element.click();
}
@Test(priority = 2)
public void update2(){
driver.findElement(By.xpath(".//*[@id='post-1139']/td[1]/strong/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
[@id='menu-posts-presentation']/a/div[3]")));
element.click();
//if i am using class name instead of x path , then how to do same procedure
in loop so my code became so small or merged..
@Test(priority = 1)
public void update1() {
driver.findElement(By.className("row-title")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
[@id='menu-posts-presentation']/a/div[3]")));
element.click();
}
@Test(priority = 2)
public void update2(){
driver.findElement(By.className("row-title")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
[@id='menu-posts-presentation']/a/div[3]")));
element.click();
}
答案 0 :(得分:0)
您必须使用findElements
来检索元素列表。然后一一循环。
List<WebElement> elements=driver.findElements(By.className("row-title"));
for (int i = 0; i < elements.size(); i++) {
elements.get(i).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
element.click();
}