一、摘要

前一段时间公司小伙伴刚刚接触自动化,遇到的一个问题,页面新创建的数据保存后,出现在table中的某个位置,并不一定是第一行还是第几行,这种情况下如何去操控它

本篇博文将介绍处理这个问题的一种方式

二、测试代码

    @Test
public void test_Table() throws Exception {//获取表单,xpath是表单的定位
WebElement tableElement=driver.findElement(By.xpath("//*[@id='app']/section/section/main/section/div[1]/div[3]/table"));
//将表单的所有tr放进列表,每个tr是表单的一行,逐行遍历
List<WebElement> rows=tableElement.findElements(By.tagName("tr"));
for (int i = 0; i < rows.size(); i++) {
//将表单的td放进list里,每个td是表单的一列,逐列遍历
List<WebElement> cols=rows.get(i).findElements(By.tagName("td"));
for (int j = 0; j < cols.size();) {
String tdText = cols.get(j).getText();
sleep(1000);
System.out.println(tdText +"\t");
//判断哪行哪列的内容包含字段"mysql01", 如果包含则进行操作
if(tdText.contains("mysql01")){
System.out.println(i+1);
System.out.println(j+1);
int row = i + 1;
//点击mysql01所在行的下拉按钮
WebElement dropdown = driver.findElement(By.xpath("//*[@id='app']/section/section/main/section/div[1]/div[3]/table/tbody/tr["+row+"]/td[6]/div/div/span"));
dropdown.click();
}break;
}
} }

实际上如果页面存在检索功能,完全可以写几步检索操作,让页面只有一条你要的数据,那么它的位置就是固定了,然后再进行操控

三、处理Table的其他方法封装

package util;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.util.NoSuchElementException;
import static util.WaitElementUtil.sleep; /*
* some method of controlling table
*
* @author: davieyang
* @create: 2018-08-05 14:04
*/
public class TableUtil {
//声明一个WebElement对象,用于存储页面的表格元素对象
private WebElement _table; //为构造函数传入页面表格元素对象参数,调用TableUtil类的settable方法,将页面表格元素赋值给TableUtil类的_table成员变量
public TableUtil (WebElement table){
setTable(table);
}
//获取页面表格对象的方法
public WebElement getTable(){
return _table;
}
//将页面表格元素赋值给TableUtil类中_table成员变量的方法
public void setTable(WebElement _table){
this._table = _table;
}
//获取表格元素的行数,查找表格元素有几个tr元素,有几个tr元素,就可以知道表格有几行,tr数量和表格行数相一致
public int getRowCount(){
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
return tableRows.size();
}
//获取表格元素的列数,使用get(0)从容器中取出表格第一行的元素,查找有几个“td”,td数量和列数一致
public int getColumnCount(){
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
return tableRows.get(0).findElements(By.tagName("td")).size();
}
//获取表格中某行某列的单元格对象
public WebElement getCell(int rowNo, int colNo)throws NoSuchElementException{
try{
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
System.out.println("行总数:" + tableRows.size());
System.out.println("行号:" + rowNo);
WebElement currentRow = tableRows.get(rowNo - 1);
List<WebElement> tableCols = currentRow.findElements(By.tagName("td"));
System.out.println("列总数:" + tableCols.size());
WebElement cell = tableCols.get(colNo-1);
System.out.println("列号:" + colNo);
return cell;
}catch (NoSuchElementException e){
throw new NoSuchElementException("没有找到相关元素");
}
}
/**
* 获得表格中某行某列的单元格中的某个页面元素对象,by参数用于定位某个表格中的页面元素,例如by.xpath("input[@type='text']")可以定义到表格中的输入框
*/
public WebElement getWebElementInCell(int rowNo, int colNo, By by)throws NoSuchElementException{
try{
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
//找到表格中的某一行,行号从0开始,例如第三行,则需要进行3-1来获取即“2”
WebElement currentRow = tableRows.get(rowNo-1);
List<WebElement> tableCols = currentRow.findElements(By.tagName("td"));
//找到表格中的某一列,因为也是从0开始,所以要找到第三列,则需要进行3-1来获取即“2”
WebElement cell = tableCols.get(colNo-1);
return cell.findElement(by);
}catch (NoSuchElementException e){
throw new NoSuchElementException("没有找到相关元素");
}
} /**
*
* @param driver 浏览器驱动
* @param row 行号
* @param column 列号
* @return 函数接受浏览器驱动,表格行数和列数,注意表头行,返回某个cell的值
*/
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));
text=table.getText();
return text;
}
}

Java&Selenium处理页面Table以及Table中随机位置的数据的更多相关文章

  1. php 从一个数组中随机获取固定数据

    <?php /* * * 通过一个标识,从一个数组中随机获取固定数据 * $arr 数组 * $num 获取的数量 * $time 随机固定标识值,一般用固定时间或者某个固定整型 * */ fu ...

  2. Java selenium web页面的滚动条操作

    摘录自:http://blog.csdn.net/iceryan/article/details/8162703 //移动到元素element对象的"顶端"与当前窗口的" ...

  3. Java Selenium - 处理页面弹出窗

    1. 得到当前窗口句柄 2. 得到所有窗口句柄 3. 循环找到目标窗口 String currentWindow = driver.getWindowHandle(); Set<String&g ...

  4. java使用htmlunit工具抓取js中加载的数据

    htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实现.这个没有界面的浏览器,运行速度 ...

  5. java基础:输出数组中指定位置的数据

  6. 需求:lr需要在一串数字中随机位置插入一个新数字的实现方式

    效果如下: 需要用到sscanf()函数:  从一个字符串中读进与指定格式相符的数据. Action() { ],s2[],s3[]; int n=atoi(lr_eval_string(" ...

  7. 数据库表A中随机X条数据满足N条件的数据插入到表B中

    select  * into c FROM a TABLESAMPLE (5 PERCENT) select top 5 per * into c from a order by newid() se ...

  8. Jmeter实现从csv文件中随机读取数据

    一.需求 参数放在csv文件中,文件格式如下,需求每次从文件中随机读取一行数据. 二.步骤 1.在csv文件中新增加一列,pl 2.新增一个配置原件-随机数,设置如下: 50是文件数据的行数 3.新增 ...

  9. Java Selenium (十二) 操作弹出窗口 & 智能等待页面加载完成 & 处理 Iframe 中的元素

    一.操作弹出窗口   原理 在代码里, 通过 Set<String> allWindowsId = driver.getWindowHandles(); 来获取到所有弹出浏览器的句柄, 然 ...

随机推荐

  1. CF1190D Tokitsukaze and Strange Rectangle

    思路: 线段树 + 扫描线. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int n ...

  2. JavaScript 真值和假值

    常见的假值有 值 说明 var a=false;  值为假 var a =0;  值为0 var a='';  值为空 var a=10/'abc' 算式错误 var a; 未赋值变量 常见的真值有 ...

  3. cenos 防火墙操作

    iptables防火墙 1.基本操作 # 查看防火墙状态 service iptables status   # 停止防火墙 service iptables stop   # 启动防火墙 servi ...

  4. linux 系统自签免费ssl证书和nginx配置

    首先执行如下命令生成一个key openssl genrsa -des3 -out ssl.key 1024 然后他会要求你输入这个key文件的密码.不推荐输入.因为以后要给nginx使用.每次rel ...

  5. 在Ubuntu中搭建Python3的虚拟环境并开始django项目

    搭建环境: 1.首先安装virtualenv: pip install virtualenv 2.创建虚拟环境:(指定安装Python3,若不写-p python3,默认安装Python2.7),en ...

  6. docker服务端与客户端通信方式

    docker的服务端与客户端间可以通过unix.tcp方式进行通信.但默认情况下,服务端只监听本地unix接口/var/run/docker.sock,所以客户端只能在服务端所在的机器上使用该unix ...

  7. 关于@service、@controller和@transactional 在spring中的位置说明

    Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_serv ...

  8. 【转】Entity Framework 6 Code First 实践系列(1):实体类配置-根据依赖配置关系和关联

    本文转自:http://www.cnblogs.com/easygame/p/3622893.html EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关 ...

  9. django websocket 实现后台日志在web端展示(+前端vue设置)

    核心代码: @accept_websocket def get_log(req): if req.is_websocket(): print('收到websocket请求') with open(se ...

  10. chartjs显示数值标签插件:chartjs-plugin-datalabels

    Getting Started #Installation #npm   npm install chartjs-plugin-datalabels --save This plugin can al ...