一、摘要

前一段时间公司小伙伴刚刚接触自动化,遇到的一个问题,页面新创建的数据保存后,出现在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. Vuetify Warning: Unable to locate target [data-app]

    今天在大改 nuxt 项目 layout 的时候,将 Vuetify 的 side nav 换成 Element 的折叠式 nav 后,发现了一个 Warning,虽然不影响使用,但是控制台输出一堆, ...

  2. [Tensorflow] 使用 Mask_RCNN 完成目标检测与实例分割,同时输出每个区域的 Feature Map

    Mask_RCNN-2.0 网页链接:https://github.com/matterport/Mask_RCNN/releases/tag/v2.0 Mask_RCNN-master(matter ...

  3. VC程序运行时间测试函数

    VC程序运行时间测试函数 介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如 ...

  4. OpenCV_CS.测试

    1.在C#中使用OpenCV(使用OpenCVSharp) - jsxyhelu - 博客园.html(https://www.cnblogs.com/jsxyhelu/p/9669583.html) ...

  5. bootstrap基础学习【菜单、按钮、导航】(四)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 浅谈>/dev/null 2>&1

    在crond计划任务.nohup中我们经常可以看到>/dev/null 2>&1,但是很多人并不理解其含义,想要真正的理解它,首先我们需要知道文件描述符的三种类型. 类型 文件描述 ...

  7. oracle-function-into时为null报错

    oracle-function-into时为null报错 create or replace function P_ADD_CUSTOMER_FOR_CSS_heyt_test(i_cust_name ...

  8. 关于日志slf4j+logback&logback.xml配置

    1.maven依赖 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <!-- <dependen ...

  9. 剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    1 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印 ...

  10. BridgeOverARoughRiver(POJ-3404)【AdHoc】

    题目链接:https://vjudge.net/problem/POJ-3404 题意:n个极限速度不同的人要过桥,一开始所有人在桥的一边,每次最多两个人同时过桥,过桥时需要用一把火炬并且全场只有一把 ...