<html>
<body>
<div id="div1">
<input name="divl1input">input>
<a href="http://www.sogou.com/">搜狗搜索a>
<img alt="div1-img1 "src="http://www.sogou.com/images/logo/new/sogou.png"
href = "http://www.sogou.com">搜狗图片img>
<input type ="button" value="查询">input>
div>
<br>br>
<div name="div2">
<input name="div2input">input>
<a href = "http://www.baidu.com">百度搜索a>
<img alt="div2-img2" src="http://www.baidu.com/img/bdlogo.png"
href= "http://www.baidu.com">百度图片img>
<input type="button" value="查询">input>
div>
<br>br>
<br>br>
<br>br>
<br>br>
<a href="http://www.sogou.com"> 搜狗搜索a><br>br>
<a href="http://www.baidu.com"> 百度搜索a>
<br>br>
<br>br>
<br>br>
<br>br>
<table width="400" border="1" id= "table">
<tr>
<td align="left" >消费项目。。。。td>
<td align="right"> one month td>
<td align="right">two monthtd>
tr>
<tr>
<td align="left" >衣服td>
<td align="right"> 1000 元 td>
<td align="right">2000 元td>
tr>
<tr>
<td align="left" >化妆品td>
<td align="right"> 10 元 td>
<td align="right">20 元td>
tr>
<tr>
<td align="left" >零食td>
<td align="right"> 120 元 td>
<td align="right">100 元 td>
tr>
<tr>
<td align="left" >总计td>
<td align="right">88880 元 td>
<td align="right">10000 元td>
tr>
table>
body>
html>
package page_elements_positioning;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Table_Positiing {
public static void main(String[] args) {
String url ="file:///C:/Users/ty/Desktop/selenium_test.html";
System.setProperty("webdriver.firefox.bin", "D:\\softerWare\\firefox\\firefox\\firefox.exe");
WebDriver driver ;
driver = new FirefoxDriver();
driver.get(url);
WebElement table =driver.findElement(By.id("table"));
List rows = table.findElements(By.tagName("tr"));
for (WebElement row : rows ){
List cols =row.findElements(By.tagName("td"));
for(WebElement col :cols ){
System.out.print(col.getText()+"\t");
}
System.out.println("");
}
System.out.println("-----------------------------------------------------------------------");
//注意" //*[@id=‘table‘]/tbody/td[1]/tr[1]"---- /tbody 是必须要加的
WebElement element =driver.findElement(By.xpath("//*[@id=‘table‘]/tbody/tr[2]/td[3]"));
//---去掉 * 号 !
WebElement element1 =driver.findElement(By.xpath("//*[@id=‘table‘]/tbody/tr[3]/td[3]"));
System.out.println("//*[@id=‘table‘]/tbody/tr[2]/td[3]----"+element.getText());
System.out.println("//[@id=‘table‘]/tbody/tr[3]/td[3]---"+element1.getText());
}
}