那么問題來了,我想通過xpath獲取這個table的每一個cell的值(比如獲取expect的值),該怎么做。
如果這個表格被改變了,增加或刪除一些行列該如何處理?
我的solution是獲取table的base xpath,這個所謂的base xpath是指這個table的第n行第m列相同的部分,然后通過傳入n,m獲取返回值
public static String tableCell(WebDriver driver,int row, int column)
{
String text = null;
//avoid get the head line of the table
row=row+1;
String xpath="//*[@id='table138']/tbody/tr["+row+"]/td["+column+"]";
WebElement table=driver.findElement(By.xpath(xpath)); //*[@id="table138"]/tbody/tr[1]/td[1]/strong
text=table.getText();
return text;
}
所以,tableCell(driver,1,2)能返回