First, theHTML table is composed of a TABLE element and one or more tr,th , or The TD element is composed.
For example:
This is a simple HTML table:
The source code is as follows:
So the question is, I want to get the value of each cell of this table through XPath (for example, get expect value), what to do.
If this table is changed, how do I add or remove a number of columns?
My solution is to get the base XPathof the table, this so-called base XPath Refers to the same portion of column m of row n of this table , and then passes in the n,m Get return value
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; }
So,TableCell (driver,1,2) will be able to return
Selenium Webdriver Processing Table