下面是两种方法
gettablevalueprd("//div[@id='Catalog']//tr",2,"Large Angelfish",0,"EST-1");
gettablevalueshopcart("//div[@id='Catalog']//tr",2,"Large Angelfish",0,"EST-1");
下面是第一种方法
public static void gettablevalueprd(String xPath,int descriptionid,String expectvalue1,int itemid,String expectedvalue2) {
//String xPath = "//div[@id='Catalog']//tr";
List<WebElement> tableList = driver.findElements(By.xpath(xPath));
System.out.println("Item ID\t\tProduct ID\t\tDescription\t\tList Price\t\tButton");
System.out.println("--------------------------------------------------------------------------------------------------");
for(int i=1;i<tableList.size();i++) {
List<WebElement> listData = driver.findElements(By.xpath(xPath+"["+(i+1)+"]/td"));
for(int j=0;j<listData.size();j++) {
System.out.print(listData.get(j).getText()+"\t\t");
}
System.out.println();
}
// description is in the 3rd(2nd index) column so we can fetch that with the index number 2.
for(int i=1;i<tableList.size();i++) {
List<WebElement> listData = driver.findElements(By.xpath(xPath+"["+(i+1)+"]/td"));
if(listData.get(descriptionid).getText().trim().equals(expectvalue1)) {
System.out.println("Description for Item ID is "+listData.get(descriptionid).getText());
}
if(listData.get(itemid).getText().trim().equals(expectedvalue2)) {
System.out.println("Item ID is "+listData.get(itemid).getText());
//to click on add to cart button
listData.get(4).click();
break;
}
}
}
另一种方法是
public static void gettablevalueshopcart(String xPath1,int descriptionid, String expectvalue1, int itemid, String expectedvalue2) {
//String xPath1 = "//div[@id='Catalog']//tr";
List<WebElement> shoppingtableList = driver.findElements(By.xpath(xPath1));
System.out.println("Item ID\t\tProduct ID\t\tDescription\t\tInstock\t\tList Price\tTotalcost\t\tButton");
System.out.println("---------------------------------------------------------------------------------------------------------------------------------");
for(int i=1;i<shoppingtableList.size();i++) {
List<WebElement> listData = driver.findElements(By.xpath(xPath1+"["+(i+1)+"]/td"));
for(int j=0;j<listData.size();j++) {
System.out.print(listData.get(j).getText()+"\t\t");
}
System.out.println();
}
// description is in the 3rd(2nd index) column so we can fetch that with the index number 2.
for(int i=1;i<shoppingtableList.size();i++) {
List<WebElement> listData = driver.findElements(By.xpath(xPath1+"["+(i+1)+"]/td"));
if(listData.get(descriptionid).getText().trim().equals(expectvalue1)) {
System.out.println("Description for Item ID is "+listData.get(descriptionid).getText());
}
if(listData.get(itemid).getText().trim().equals(expectedvalue2)) {
System.out.println("Item ID is "+listData.get(itemid).getText());
break;
}
}
}
**这里我要两个比较表**中的两个值