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. linux三尖刀

    序 我们都知道,一个可执行程序的基本的生命过程是如此的: (编辑)源文件--->(编译)目标文件--->(链接)可执行文件--->(调试排错)稳定执行 所以,在这个过程中,我们很容易 ...

  2. HDU 4826 (分类DP)

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

  3. 兼容360模式自动播放视频【需要flvpalyer.swf】

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  4. 在页面和请求中分别使用XML Publisher生成PDF报表且自动上传至附件服务器

    两个技术要点: 1.使用TemplateHelper.processTemplate方法生成目标PDF的InputStream流,再使用ftp中上传流的方法将其上传至附件服务器. 2.在请求中调用AM ...

  5. Delphi 简体 繁体 转换

    http://delphi.ktop.com.tw/board.php?cid=30&fid=69&tid=104986 試看看 這個是豬寶寶從網路上抄來的 檢視純文字版列印? fun ...

  6. Failed to start Docker Application Container

    [root@localhost ~]# systemctl status docker.service ● docker.service - Docker Application Container ...

  7. 关于CMD中延迟环境变量嵌套的实现方法

    在我昨天做的一个bat中(自动按日期重命名文件名)涉及到这方面的问题 以前涉及到这里时就想别的办法替代过去,今天好好扒出来说说: 实现变量嵌套的2种方法: 1,使用call实现变量嵌套 变量嵌套:即在 ...

  8. WebGL编程指南案例解析之绘制三角形

    //案例3.绘制三角形,将顶点数据存到缓冲区对象(gl.ARRAY_BUFFER)中,然后顶点着色器从里面读数据(3个顶点) //顶点着色器中去掉gl_PointSize = 10.0,绘制三角不能设 ...

  9. 64位的ubuntu14.04 LTS安装 Linux交叉编译工具链及32位“ia32-libs”依赖库

    ubuntu又迎来了其新一代的长期支持版本 14.04 LTS,其带来了许多令人期待的新特新,遂决定进行升级. 装好了64位版本及安装 Linux交叉编译工具链 运行GCC,${CROSS_COMPI ...

  10. [转] .net软件反编译笔记

    原文地址:http://blog.csdn.net/three_bird/article/details/51433734 在软件的破解及源码获取及重新编译的道路上会遇到一些问题,书此备查. 大名鼎鼎 ...