html table是由 table 元素以及一个或多个 tr、th 或 td 元素组成。如下:

HTML源码如下:

<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>

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

Java代码:

    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+"]";
try{
WebElement table=driver.findElement(By.xpath(xpath)); //*[@id="table138"]/tbody/tr[1]/td[1]/strong
text=table.getText();
}catch(NoSuchElementException e){
System.out.println("超出table边界值");
}
return text;
}

也可以通过 tr,td来写

    public static String tableCell2(WebDriver driver,int row, int column) {
String text = null;
//avoid get the head line of the table
//row=row+1;
try{
List<WebElement> rowCounts = driver.findElements(By.tagName("tr"));
WebElement currentRow = rowCounts.get(row-1);
List<WebElement> td = currentRow.findElements(By.tagName("td"));
WebElement cell = td.get(column-1);
text=cell.getText();
}catch(IndexOutOfBoundsException e){
System.out.println("超出table边界值");
}
return text;
}

Python代码,这里要将row/column转为str

def tablecell(driver,row ,column):
row = row + 1
xpath = "//*[@id='table138']/tbody/tr["+str(row)+"]/td["+str(column)+"]";
table = driver.find_element_by_xpath(xpath)
return table.text

示例:

JAVA代码:

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 getTableValue { public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("F:\\table.html");
System.out.println(tableCell(driver, 1, 2));
driver.quit();
}
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+"]";
try{
WebElement table=driver.findElement(By.xpath(xpath)); //*[@id="table138"]/tbody/tr[1]/td[1]/strong
text=table.getText();
}catch(NoSuchElementException e){
System.out.println("超出table边界值");
}
return text;
}
}

输出为:

Python代码

def tablecell(driver,row ,column):
row = row + 1
xpath = "//*[@id='table138']/tbody/tr["+str(row)+"]/td["+str(column)+"]";
table = driver.find_element_by_xpath(xpath)
return table.text from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("F:\\table.html")
print(tablecell(driver, 1, 2))
driver.quit()

输出为:

Selenium Webdriver——处理Table的更多相关文章

  1. Selenium WebDriver 处理table

    首先,html table是由 table 元素以及一个或多个 tr.th 或 td 元素组成. for example: 这是一个简单的html table: 源码如下: <html> ...

  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. Spring MVC配置静态资源和资源包

    Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...

  2. 转载 ORACLE中实现表变量的方法

    源文地址:http://blog.itpub.net/750077/viewspace-2134222/ 经常看到SQLSERVER 中用表变量类型的方式就能做到缓存一个比较大的中间结果, 然后再对这 ...

  3. rabbitmq 对多服务器p2p模式配置的一个测试

    一直对rabbitmq p2p 模式的多服务器下做相同配置的 各个服务器数据接受情况比较好奇 今天有空测试了下 xml 文件 <?xml version="1.0" enco ...

  4. [转载]java中的标号:outer的作用

    转载自:http://blog.sina.com.cn/s/blog_6f8bd746010136yr.html 标号label 标号提供了一种简单的break语句所不能实现的控制循环的方法,当在循环 ...

  5. Linux:关于设置PS1提示符输入长命令格式出现的问题及解决

    关于设置PS1提示符命令输出格式出现的问题解决 正确的格式 \[\e[;;32m\]xxxx #如果只是改变提示符而不改变命令,在后面加一个结束符. \[\e[;;32m\]xxxx \[\e[0m ...

  6. 【转载】你真的会浮点数与整型数的"互转"吗?

    看了标题,你是不是觉得这TM是哪个iOS彩笔写的入门文章.好的,那咱们先来看看几个例题,看看你有没有白白点进来! int main() { float a = -6.0; int *b = & ...

  7. PyAlgoTrade Hello World 第一个程序(一)

    本教程的目标是快速介绍PyAlgoTrade.PyAlgoTrade的目标是帮助您实现股票交易策略.假设您有一个交易策略的想法,并且您希望使用历史数据进行评估,并查看其行为方式,那么PyAlgoTra ...

  8. redis安装全过程

    1. 从官网上下载redis. 2.安装gcc 3.进入./redis/src目录下make MALLOC =libc 4.遇到的问题 Redis简介: Redis是一个开源的使用ANSI C语言编写 ...

  9. mcc,mex,mbuild

    mcc将M文件转换成C/C++文件和相应的MEX包裹文件(需要Matlab编辑器),但在特定条件下可以自动调用mbuild或者mexmex将C/C++文件(入口函数为mexfunction())编译成 ...

  10. HDU 4764:Stone(巴什博弈)

    Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...