首先,html table是由 table 元素以及一个或多个 tr、th 或 td 元素组成。

for example:

这是一个简单的html table:

源码如下:

<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>for selenium test </title> </script>
</head>
<body>
<div align="center"> <h4 align="left"> table head:</h4>
<table border="2" width="90%" id="table138" bordercolorlight="#CCCCCC" cellspacing="0" cellpadding="0" bordercolordark="#CCCCCC" style="border-collapse: collapse">
<tr align="center">
<td height="26" width="10%" align="center" bgcolor="#CCEEAA" ><Strong>Test Case ID</Strong></td>
<td height="26" width="35%" align="center" bgcolor="#EEEEAA" ><Strong>Steps</Strong></td>
<td height="26" width="30%" align="center" bgcolor="#00EEEE" ><Strong>Expect</Strong></td>
<td height="26" align="center" bgcolor="#EE00EE" ><Strong>Actual</Strong></td>
<td height="26" align="center" bgcolor="#00EE00" ><Strong>PASS/FAIL</Strong></td>
</tr>
<tr align="center">
<td height="26" align="center" bgcolor="#CCEEAA">ENT#-12345</td>
<td height="26" 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>
<td height="26" align="left" bgcolor="#00EEEE" >"Selenium - Web Browser Automation" link be the first of the search result</td>
<td height="26" align="left" bgcolor="#EE00EE" >Selenium - Web Browser Automation is appear the page,but is not the first link</td>
<td height="26" align="center" bgcolor="#00EE00" >FAIL</td>
</tr>
</tr>
<tr align="center">
<td height="26" align="center" bgcolor="#CCEEAA">ENT#-12346</td>
<td height="26" align="left" bgcolor="#EEEEAA" >1.click the "Selenium - Web Browser Automation" link</br>2.wait for page load</td>
<td height="26" align="left" bgcolor="#00EEEE" >open the official home page of selenium</td>
<td height="26" align="left" bgcolor="#EE00EE" >selenium home page is load </td>
<td height="26" align="center" bgcolor="#00EE00" >FAIl</td>
</tr>
</tr>
<tr align="center">
<td height="26" align="center" bgcolor="#CCEEAA">ENT#-12347</td>
<td height="26" align="left" bgcolor="#EEEEAA" >1.click baidu snapshot of selenium web page </br>2. wait for the page load</td>
<td height="26" align="left" bgcolor="#00EEEE" >the snapshot web page can be show up</td>
<td height="26" align="left" bgcolor="#EE00EE" >the snapshot web page is show up</td>
<td height="26" align="center" bgcolor="#00EE00" >PASS</td>
</tr>
</table>
</div> </body>
</html>

那么问题就来了,我想通过xpath获取这个table的每一个cell的值(比如获取expect的值),该怎么做。

如果这个表格被改变了,增加或删除一些行列该如何处理?

我的solution是获取table的base xpath,这个所谓的base xpath是指这个table的第n行第m列相同的部分,然后通过传入n,m获取返回值

    public static String tableCell(WebDriver driver,int row, int column)
{
String text = null;
//avoid get the head line of the table
row=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]/strong
text=table.getText();
return text; }

所以,tableCell(driver,1,2)就能返回

Selenium WebDriver 处理table的更多相关文章

  1. Selenium Webdriver——处理Table

    html table是由 table 元素以及一个或多个 tr.th 或 td 元素组成.如下: HTML源码如下: <html> <head> <meta http-e ...

  2. Selenium Webdriver——Table类封装

    WebTable.java import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebEl ...

  3. selenium webdriver 右键另存为下载文件(结合robot and autoIt)

    首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...

  4. Selenium_用selenium webdriver实现selenium RC中的类似的方法

    最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...

  5. WebDriver获取table的内容(通过动态获取Table单元格的TagName对其innerHTML值进行获取)

    import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedHashMap;import java.util ...

  6. selenium webdriver (python)的基本用法一

    阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...

  7. Selenium Webdriver 自动化测试开发常见问题(C#版)

    转一篇文章,有修改,出处http://www.7dtest.com/site/blog-2880-203.html 1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver ...

  8. Selenium WebDriver Api 知识梳理

    之前一直没有系统的梳理WebDriver Api的相关知识,今天借此机会整理一下. 1.页面元素定位 1.1.8种常用定位方法 # id定位 driver.find_element_by_id() # ...

  9. Selenium Webdriver概述(转)

    Selenium Webdriver https://www.yiibai.com/selenium/selenium_overview.html# webdriver自动化俗称Selenium 2. ...

随机推荐

  1. step 3 socket

    socket 网络通讯三要素 IP地址(主机名) 网络中设备的标示 不易记忆,可以用主机名 本地回环地址:127.0.0.1 主机名:localhost 每台计算机都有一个 127.0.0.1 如果 ...

  2. Win7与XP共享互相访问及共享注意事项!

    win7共享方法和XP类似,主要需要检查以下操作: 1,首先将Guest账户打开 2,右击文件夹-属性-共享选项-高级共享 3,将共享文件√打上,应用-确定即可! 4,查看自己IP(开始-运行-cmd ...

  3. 【转】Eclipse里项目名有红叉,但是展开后里面又没有红叉叉

    Eclipse里项目名有红叉,但是下面的每一个文件都没有红叉 有三种可能: 1:classpath有问题 2:编译级别有问题 3:jar包有问题,我碰过从maven上获取了问题jar包,工程里提示能找 ...

  4. 牡丹江.2014B(图论,树的直径)

    B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & ...

  5. 【知乎网】Linux IO 多路复用 是什么意思?

    提问一: Linux IO多路复用有 epoll, poll, select,知道epoll性能比其他几者要好.也在网上查了一下这几者的区别,表示没有弄明白. IO多路复用是什么意思,在实际的应用中是 ...

  6. Swift实战之2048小游戏

    上周在图书馆借了一本Swift语言实战入门,入个门玩一玩^_^正好这本书的后面有一个2048小游戏的实例,笔者跟着实战了一把. 差不多一周的时间,到今天,游戏的基本功能已基本实现,细节我已不打算继续完 ...

  7. MVC后台传dt数据

    //MVC后台传dt数据 public JsonResult TeacherVoteInfo([FromBody]Teacher_VoteModel model) { string tname = m ...

  8. Android 如何在Eclipse中查看Android API源码 及 support包源码

    当我们阅读android API开发文档时候,上面的每个类,以及类的各个方法都是已经写好的方法和控件,可是我们只是在搬来使用,不知道它的原理,它是如何被实现的.android系统是开源的,所以谷歌官方 ...

  9. iOS开发——高级篇——如何集成支付宝SDK

    一.什么是支付宝 第三方支付平台 和内购非常相似内购是用户将钱付款给苹果,之后苹果分成给商户支付宝是用户将钱付款给支付宝,之后支付宝将钱转入我们的账户 使用支付宝前提购买的物品必须是和应用程序无关的. ...

  10. View的3种测量模式

    EXACTLY:表示设置了精确的值,一般当childView设置其宽.高为精确值.match_parent时,ViewGroup会将其设置为EXACTLY: AT_MOST:表示子布局被限制在一个最大 ...