`

selenium webdriver学习(十四)------------如何处理table

阅读更多

以前在selenium RC 里面有一个getTable方法,是得到一个单元格中的文本。其详细描述如下:

 

/** Gets the text from a cell of a table. The cellAddress syntax tableLocator.row.column
, where row and column start at 0.
@param tableCellAddress a cell address, e.g. "foo.1.4"
@return the text from the specified cell
*/
String getTable(String tableCellAddress);

 

就是传入一个参数,这个参数的格式必须是tableLocator.row.column,如"foo.1.4",foo用于得到table对象,1.4代表在table里第1行第4列。行、列从0开始。

 

在selenium webdriver里,没有这样的方法,也就是说没有专门操作table的类。但我们可以自己封闭一个,这并不难。以上面的getTable方法为例,我们自己也可以创建这样功能的一个方法。

 

public String getCellText(By by,String tableCellAddress)

 

 我叫它getCellText,它有两个参数,第一个是By对象用于得到table对象, tableCellAddress 如"1.4",代表在table里第1行第4列。行、列从0开始。

以下面html代码为例:

 

<html>
    <head>
        <title>Table</title>
        
    </head>
    <body>
        <table border="1" id="myTable">
			<tr>
				<th>Heading(row 0 ,cell 0)</th>
				<th>Another Heading(row 0 ,cell 1)</th>
				<th>Another Heading(row 0 ,cell 2)</th>
			</tr>
			<tr>
				<td>row 1, cell 0</td>
				<td>row 1, cell 1</td>
				<td>row 1, cell 2</td>
			</tr>
			<tr>
				<td>row 2, cell 0</td>
				<td>row 2, cell 1</td>
				<td>row 2, cell 2</td>
			</tr>
		</table>
    </body>
</html>

 

 

 

示例代码如下:

 

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Table {

	/**
	 * @author gongjf
	 */
	private WebDriver driver;
	Table(WebDriver driver){
		this.driver = driver;
	}
	
	/** 从一个table的单元格中得到文本值. 参数tableCellAddress的格式为
	row.column, 行列从0开始.
	@param by  用于得到table对象
	@param tableCellAddress 一个单元格地址, 如. "1.4"
	@return 从一个table的单元格中得到文本值
	*/
	public String getCellText(By by,String tableCellAddress) {
		//得到table元素对象
		WebElement table = driver.findElement(by);
		//对所要查找的单元格位置字符串进行分解,得到其对应行、列。
		int index = tableCellAddress.trim().indexOf('.');
		int row =  Integer.parseInt(tableCellAddress.substring(0, index));
		int cell = Integer.parseInt(tableCellAddress.substring(index+1));
		//得到table表中所有行对象,并得到所要查询的行对象。
		 List<WebElement> rows = table.findElements(By.tagName("tr"));
		 WebElement theRow = rows.get(row);
		 //调用getCell方法得到对应的列对象,然后得到要查询的文本。
		 String text = getCell(theRow, cell).getText();
		 return text;
	}
	private WebElement getCell(WebElement Row,int cell){
		 List<WebElement> cells;
		 WebElement target = null;
		 //列里面有"<th>"、"<td>"两种标签,所以分开处理。
		 if(Row.findElements(By.tagName("th")).size()>0){
			cells = Row.findElements(By.tagName("th"));
			target = cells.get(cell);
		 }
		 if(Row.findElements(By.tagName("td")).size()>0){
			cells = Row.findElements(By.tagName("td"));
			target = cells.get(cell);
		 }
		return target;
		 
	}
	
	
	public static void main(String[] args) {
		 WebDriver driver;
		 System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
		 driver = new FirefoxDriver();
		 driver.get("file:///C:/Documents and Settings/Gongjf/桌面/selenium_test/table.html");
	
		 Table table = new Table(driver);
		 By by = By.id("myTable");
		 String address = "0.2";
	 
		 System.out.println(table.getCellText(by, address));
		
		
	}

}

 运行代码将输出

 

Another Heading(row 0 ,cell 2)
 

 

 ps:   这里我只是以得到一个table中单元格的文本为例,但是从代码可以看出,对table的基本操作都有涉及到。有用到的同学可以自己包装一个完整的table类。

分享到:
评论

相关推荐

    Selenium WebDriver Practical Guide 最新 原版

    Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...

    selenium webdriver 3 practical guide 第二版

    Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...

    Selenium.WebDriver.Recipes.in.Csharp.2nd.Edition.1484217

    Solve your Selenium WebDriver problems with this quick guide to automated testing of web applications with Selenium WebDriver in C#. Selenium WebDriver Recipes in C#, Second Edition contains hundreds ...

    Selenium.Essentials.1784394335

    Get to grips with automated web testing with the amazing power of Selenium WebDriver About This Book Utilize Selenium WebDriver features for automation testing using outstanding techniques and ...

    webdriver处理table模块

    selenium-webdriver处理table模块的ruby版本。

    Packt.Learn.Selenium.rar

    Selenium WebDriver 3.x is an open source API for testing both browser and mobile applications. With the help of this book, you can build a solid foundation and can easily perform end-to-end testing on...

    Selenium.Testing.Tools.Cookbook.2nd.Edition.178439251

    Over 90 recipes to help you build and run automated tests for your web applications with Selenium WebDriver About This Book Learn to leverage the power of Selenium WebDriver with simple examples that...

    webdriver中对于页面列表元素table,tr、td的智能读取

    webtable类-对于页面中列表含有table的,获取元素对象的方法类

    爬取全国空气质量监测网代码.py

    使用selenium的webdriver.firefox(),driver.execute_script("return items") 数据可获得。 仍遇到的问题:----------------------------------------- 爬取一个网页可获得数据,但是连续的获取网页,会出现两个错误...

    Android代码-webdrivermanager

    chromedriver, geckodriver, etc.) required by Selenium WebDriver. Table of contents Motivation WebDriverManager as Java dependency Basic usage Examples Driver versions WebDriverManager API ...

    Ski-Scraper:抓取ski-resort-stats.com的漫游器

    使用由提供的Selenium Webdriver和Multi-Webbing模块,并通过pip安装它: pip install multi_webbing 运行! 实例机器人,其代码为: skiscraper = ski_scraper(n) n是可选的以限制刮擦的行数,如果保留为空白,...

    robotframework_selenium2library:为windows handle和table添加qitao关键字的fork

    用于机器人框架的 Selenium 2 (WebDriver) 库这个前叉有什么新东西修复了 NoSuchWindowException 问题: 增强 browser.select_window() 而不破坏它的现有用法: a. let it accept argument 'main' or '' or None to ...

    selenium自动化测试中的属性信息获取

    我们再做UI自动化过程中经常要...from selenium import webdriver import time driver = webdriver.Chrome() driver.get(‘http://www.bcbxhome.com’) time.sleep(1) title = driver.title print(title) 复制代码 2、

    python 翻译word表格小程序

    背景 原是弱电集成的设计员,纠结很久后参加了python培训机构转职后的一员小白,由于一次工作中需要翻译一份近100页word表格,纯手工翻译大概三个小时,为了解决这种重复又耗时的劳动,并...from selenium.webdriver.

    解决webdriver.Chrome()报错:Message:’chromedriver’ executable needs to be in Path

    在进行爬虫爬取淘宝商品信息时候,利用selenium来模拟浏览器进行爬取时遇到了这个问题: selenium.common.exception.WebDriverException:Message:’chromedriver’ executable needs to be in Path 详细如下图所示...

Global site tag (gtag.js) - Google Analytics