Selenium_Java实例代码(3)

package cn.selenium2.test;

import static org.junit.Assert.*;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;
import org.testng.Assert;

public class Selenium2_Test {
WebDriver driver;
@Test
public void tableTest() throws Exception {
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
//WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);
System.setProperty("webdriver.ie.driver","E:\\SeleniumIDE\\IEDriverServer_Win32_2.35.1\\IEDriverServer.exe");
       driver = new InternetExplorerDriver(ieCapabilities);
       JavascriptExecutor jse = (JavascriptExecutor)driver;
driver.get("http://www.w3school.com.cn/html/html_tables.asp");
//首先得到所有tr的集合
List<WebElement> rows =
driver.findElements(By.cssSelector(".dataintable tr"));
//验证表格的行数
assertEquals(11,rows.size());
//打印出所有单元格的数据
for (WebElement row : rows) {
//得到当前tr里td的集合
List<WebElement> cols =
row.findElements(By.tagName("td"));
for (WebElement col : cols) {
System.out.print(col.getText());//得到td里的文本
}
System.out.println();
}
}
@AfterClass
  public void tearDown(){
driver.quit();
}
}

你可能感兴趣的:(Selenium_Java实例代码(3))