Selenium WebDriver处理Table

简介:
首先,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)就能返回
最新内容请见作者的GitHub页:http://qaseven.github.io/
相关文章
|
8月前
|
Web App开发 JavaScript
2021最新Selenium真正绕过webdriver检测
2021最新Selenium真正绕过webdriver检测
192 0
|
1天前
|
Java 测试技术 持续交付
深入理解与应用Selenium WebDriver进行自动化测试
【4月更文挑战第25天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加速市场发布的关键步骤。Selenium WebDriver作为业界广泛采用的自动化测试工具之一,提供了一种灵活且高效的方式来模拟用户与Web应用程序交互。本文将探讨Selenium WebDriver的核心概念、架构以及实际应用中的技巧和最佳实践。通过深入分析其工作原理及常见问题解决方案,旨在帮助测试工程师提升测试效率,确保测试结果的准确性和可靠性。
|
9天前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十三)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换下卷(详细教程)
【4月更文挑战第15天】本文介绍了如何使用Selenium进行浏览器窗口切换以操作不同页面元素。首先,获取浏览器窗口句柄有两种方法:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口句柄。在项目实战部分,给出了一个示例,展示了在百度首页、新闻页面和地图页面之间切换并输入文字的操作。最后,文章还探讨了在某些情况下可能出现的问题,并提供了一个简单的本地HTML页面示例来演示窗口切换的正确操作。
38 0
|
9月前
|
Web App开发 数据采集 JavaScript
Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
Youtube 是一个非常流行的视频分享平台,有时候我们可能想要爬取一些视频的信息,比如标题、播放量、点赞数等。但是有些信息并不是直接显示在网页上的,而是需要我们将鼠标悬停在某个元素上才能看到,比如视频的时长、上传时间等。这些信息被称为悬停文本,它们是通过 JavaScript 动态生成的,所以我们不能用普通的 HTML 解析方法来获取它们。那么,我们该如何用爬虫来获取 Youtube 的悬停文本呢?本文将介绍一种方法,使用 Selenium Chrome Webdriver 来模拟浏览器操作,获取 Youtube 的悬停文本。
118 0
Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
|
Web App开发 JavaScript 测试技术
Selenium Webdriver 简易教程2
Selenium Webdriver 简易教程2
93 0
|
Web App开发 前端开发 JavaScript
Selenium Webdriver 简易教程
Selenium Webdriver 简易教程
95 0
|
JavaScript
selenium webdriver执行远程 第三方js解决方案
selenium webdriver执行远程 第三方js解决方案
selenium webdriver执行远程 第三方js解决方案
|
API 索引
selenium源码通读·13 |webdriver/support分析
selenium源码通读·13 |webdriver/support分析
88 0
selenium源码通读·13 |webdriver/support分析
|
移动开发 JavaScript 前端开发
selenium源码通读·12 |webdriver/remote分析
selenium源码通读·12 |webdriver/remote分析
155 0
selenium源码通读·12 |webdriver/remote分析
|
存储 API
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
68 0
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析

热门文章

最新文章