- 浏览: 319049 次
- 性别: Icon_minigender_1
- 来自: 上海
最新评论
-
yan372397390:
请问这几行命令是在哪里输入的
Genymotion模拟器安装Genymotion-ARM-Translation变copy的解决办法 -
littlevine:
...
"WebDriverException: Cannot find firefox binary in PATH."的解决方法 -
jujis008:
楼主, 这selenium用的是jdk自带的log,所以在% ...
PhantomJSDriver怎么设置在console里不输出运行信息 -
qi_ling2005:
OnTheRoad_lee 写道 String[] phant ...
PhantomJSDriver怎么设置在console里不输出运行信息 -
OnTheRoad_lee:
String[] phantomArgs = new Str ...
PhantomJSDriver怎么设置在console里不输出运行信息
selenium webdriver学习(十四)------------如何处理table
- 博客分类:
- Selenium-webdriver
以前在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 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...
- ruby+selenium-webdriver测试-测试用例源代码
Ruby 和 Selenium-Webdriver 是一种强大的组合,用于自动化 Web 应用程序的测试。这篇博客主要探讨了如何利用这两种...通过学习和理解这个脚本,你可以进一步提升在 Ruby 和 Selenium-Webdriver 结合使用时的测试能力。
- 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版本。
- seleniumwebdriver
### Selenium WebDriver 学习知识点概览 #### 1. Selenium WebDriver 概述 - **定义**: Selenium WebDriver 是一种用于自动化 Web 测试的工具,能够直接与浏览器交互,并且支持多种编程语言,例如 Java、Python、C#...
- SeleniumWebDriver菜谱
在自动化测试领域,Selenium WebDriver 是一款非常强大的工具,它允许开发者模拟用户行为,对网页进行操作和测试。本篇文章将深入探讨如何使用Selenium WebDriver的`findElement`和`findElements`方法来定位网页上的...
- 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...
- python selenium运行失败常见错误.docx
错误信息:org.openqa.selenium.NoSuchElementException: Unable to locate element:{"method":"xpath","selector":"//*[@id='listDiv']/table[1]/tbody/tr[12]/td[11]/a[2]/img"} 解决方案: * 检查定位方法是否...
- 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...
- 開源自動化測試工具Selenium-IDE
Selenium是一个基于Web的自动化测试工具的系列,包括Selenium IDE、Selenium Remote Control、Selenium WebDriver等,主要用于Web应用程序的自动化功能测试。Selenium支持多种浏览器和操作系统,为用户提供了一整套...
- webdriver中对于页面列表元素table,tr、td的智能读取
本篇文章将详细讲解如何在 WebDriver 中有效地读取和操作页面中的表格元素,如 `table`,`tr` 和 `td`。 首先,`table` 元素是 HTML 中用于展示数据的结构化组件,通常用于创建二维表格。`tr`(table row)元素定义...
- RobotFramework-Selenium2Library中文版_V1.1.pdf
### RobotFramework-Selenium2Library中文版_V1.1.pdf 关键知识点解析 #### 一、简介 本文档主要介绍了RobotFramework结合Selenium2Library进行自动化测试的方法与实践。RobotFramework是一个通用的自动化测试框架...
- Selenium使用文档
### Selenium 使用技术文档知识...通过学习 Selenium 的各种组件和技术,测试人员可以有效地提高测试效率,确保 Web 应用的质量。本书提供的丰富案例和实战技巧对于初学者和有经验的测试工程师来说都是非常宝贵的资源。
- 《自动化功能测试(selenium)》学生实验报告
from selenium.webdriver.support import expected_conditions as EC # 读取测试数据 def readData(): book = xlrd.open_workbook('12306_dateinfo.xls', 'r') table = book.sheet_by_index(0) newRows = [] ...
- 爬取全国空气质量监测网代码.py
使用selenium的webdriver.firefox(),driver.execute_script("return items") 数据可获得。 仍遇到的问题:----------------------------------------- 爬取一个网页可获得数据,但是连续的获取网页,会出现两个错误...
- selenium(jpa+jdbc)
Selenium的核心组件包括Selenium WebDriver和Selenium IDE。 【JPA(Java Persistence API)】 Java Persistence API是Java官方提供的一个ORM(对象关系映射)框架,用于简化Java应用与数据库之间的交互。JPA通过...
- Selenium八种定位元素
本文介绍了使用XPath表达式中的`starts-with`、`contains`、`descendant`、`ancestor`和`text()`函数以及Selenium WebDriver提供的其他七种常见定位方式来定位网页元素的方法。不同的定位策略适用于不同的情况,理解...
-
selenium github doc
2015年07月21日 16:26 1132selenium 官网被墙, 看不了doc, 可以去下面这个 ... -
selenium webdriver学习(二十二)------------XVFB实现selenium在linux上无界面运行安装篇
2015年03月12日 12:25 13907selenium在linux上无界面 ... -
PhantomJSDriver怎么设置在console里不输出运行信息
2014年11月10日 15:50 2448每次运行PhantomJSDriver都输出一堆的INFO、 ... -
selenium webdriver 常见问题
2012年07月10日 18:31 0注我下面说到的webdriver就是指selenium web ... -
selenium和webdriver合并的原因
2012年07月04日 10:48 3453selenium和webdriver合并的原因,传送门 -
selenium webdriver学习(二十一)------------Selenium Grid深入学习
2012年06月29日 18:35 4924应网友要求写一个用Selenium Grid控制多系统多浏览器 ... -
selenium webdriver学习(二十)------------Selenium Grid
2012年06月27日 18:37 5944Selenium Grid允许同时并行 ... -
selenium webdriver学习(十九)-------我们的构建文件CrazyFunBuild (译)
2012年06月14日 10:50 2353原文:CrazyFunBuild ... -
selenium webdriver学习(十八)----------构建webdriver
2012年05月22日 16:03 4424准备环境 对所有版� ... -
selenium webdriver学习(十七)----------把selenium项目同步到本地eclipse
2012年05月11日 16:00 4052这里主要是想把selenium的整个项目同步到eclipse的 ... -
selenium webdriver学习(十六)----------用selenium webdriver实现selenium RC中的类似的方法
2012年05月11日 15:35 4880最近想总结一下学习selenium webdriver的情况, ... -
selenium webdriver学习(十五)------------如何处理FirefoxProfile
2012年04月10日 18:29 9157这一节主要涉及 selenium webdriver处理Fir ... -
selenium webdriver学习(十三)------------如何利用Actions类模拟鼠标和键盘的操作
2012年03月29日 12:36 12154在selenium webdriver学习(十)------- ... -
selenium webdriver学习(十二)------------如何利用selenium-webdriver截图
2012年03月26日 16:29 5948在自动化测试中常常会用到截图功能。最近用了一下selenium ... -
学习selenium-webdriver一些网站
2012年03月20日 16:49 0http://www.aosabook.org/en/sele ... -
selenium webdriver学习(十一)------------如何等待页面元素加载完成
2012年03月14日 18:25 15603web的自动化测试中,我们经常会遇到这样一种情况:当我们的程 ... -
selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面
2012年03月13日 13:09 5972Q群里有时候会有人问,selenium webdriver怎 ... -
selenium webdriver学习(九)------------如何操作cookies
2012年03月12日 17:45 5292Web 测试中我们经常会接触到Cookies,一个Cookie ... -
selenium webdriver学习(八)------------如何操作select下拉框
2012年03月12日 16:06 10294下面我们来看一下selenium webdriver是如何来处 ... -
selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框
2012年03月12日 14:08 10422alert、confirm、prompt这样的js对话框在se ...