An HTML table is made up of a TABLE element and one or more tr, TH, or TD elements. As follows:
The HTML source code is as follows:
<HTML><Head><Metahttp-equiv= "Content-language"content= "ZH-CN"><Metahttp-equiv= "Content-type"content= "text/html; charset=gb2312"><title>For selenium test</title></Script></Head><Body><DivAlign= "Center"> <h4Align= "Left">Table Head:</h4> <TableBorder= "2"width= "90%"ID= "table138"Bordercolorlight= "#CCCCCC"cellspacing= "0"cellpadding= "0"Bordercolordark= "#CCCCCC"style= "Border-collapse:collapse"> <TRAlign= "Center"> <TDHeight= "+"width= "10%"Align= "Center"bgcolor= "#CCEEAA" ><Strong>Test Case ID</Strong></TD> <TDHeight= "+"width= "35%"Align= "Center"bgcolor= "#EEEEAA" ><Strong>Steps</Strong></TD> <TDHeight= "+"width= "30%"Align= "Center"bgcolor= "#00EEEE" ><Strong>Expect</Strong></TD> <TDHeight= "+"Align= "Center"bgcolor= "#EE00EE" ><Strong>Actual</Strong></TD> <TDHeight= "+"Align= "Center"bgcolor= "#00EE00" ><Strong>Pass/fail</Strong></TD> </TR> <TRAlign= "Center"> <TDHeight= "+"Align= "Center"bgcolor= "#CCEEAA">ent#-12345</TD> <TDHeight= "+"Align= "Left"bgcolor= "#EEEEAA" >1.open baidu.com, wait for the page load</BR>2.enter "Selenium" in the input box "</BR>3.click Search button</TD> <TDHeight= "+"Align= "Left"bgcolor= "#00EEEE" >"Selenium-web Browser Automation" link be the first of the search result</TD> <TDHeight= "+"Align= "Left"bgcolor= "#EE00EE" >Selenium-web Browser Automation is appear the page,but are not the first link</TD> <TDHeight= "+"Align= "Center"bgcolor= "#00EE00" >FAIL</TD> </TR> </TR> <TRAlign= "Center"> <TDHeight= "+"Align= "Center"bgcolor= "#CCEEAA">ent#-12346</TD> <TDHeight= "+"Align= "Left"bgcolor= "#EEEEAA" >1.click the "selenium-web Browser Automation" link</BR>2.wait for page load</TD> <TDHeight= "+"Align= "Left"bgcolor= "#00EEEE" >Open the official home page of Selenium</TD> <TDHeight= "+"Align= "Left"bgcolor= "#EE00EE" >Selenium home page is load</TD> <TDHeight= "+"Align= "Center"bgcolor= "#00EE00" >FAIl</TD> </TR> </TR> <TRAlign= "Center"> <TDHeight= "+"Align= "Center"bgcolor= "#CCEEAA">ent#-12347</TD> <TDHeight= "+"Align= "Left"bgcolor= "#EEEEAA" >1.click Baidu snapshot of Selenium Web page</BR>2. Wait for the page load</TD> <TDHeight= "+"Align= "Left"bgcolor= "#00EEEE" >The snapshot Web page can be show up</TD> <TDHeight= "+"Align= "Left"bgcolor= "#EE00EE" >The snapshot Web page is show up</TD> <TDHeight= "+"Align= "Center"bgcolor= "#00EE00" >PASS</TD> </TR> </Table> </Div> </Body></HTML>
Gets the base xpath,base XPath of the table that is the same part of column m of row N of this table, and then gets the return value by passing in the N,m
Java code:
Public StaticString TableCell (Webdriver driver,intRowintcolumn) {String text=NULL; //avoid get the head line of the tableRow=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]/strongtext=Table.gettext (); returntext; }
Example:
Java code:
Python code
Selenium webdriver--Processing Table