简体   繁体   English

在Selenium Webdriver中的复杂表中查找数据

[英]Locating data in a complex table in Selenium Webdriver

I am currently trying to drill down on a user in a table full of users using Selenium webdriver, I have worked out how to iterate through the table but I'm having trouble actually selecting the person I want. 我目前正在尝试使用Selenium Webdriver在满是用户的表中向下钻取一个用户,我已经解决了如何遍历表的问题,但实际上在选择我想要的人时遇到了麻烦。

Here is the HTML (modified with X's due to it not being my data) 这是HTML(由于不是我的数据,因此用X进行了修改)

<table id="XXXXXXXXX_list" cellspacing="0" cellpadding="0" style=" border:0px black solid;WIDTH:100%;">
<tbody>
<tr cellspacing="0" style="height: 16px;">
<tr>
<tr onclick="widgetListView_onClick('XXXX_list',1,this,event)">
<tr onclick="widgetListView_onClick('XXXX_list',2,this,event)">
<tr onclick="widgetListView_onClick('XXXX_list',3,this,event)">
<tr onclick="widgetListView_onClick('XXXX_list',4,this,event)">
<tr onclick="widgetListView_onClick('XXXX_list',5,this,event)">
<tr onclick="widgetListView_onClick('XXXX_list',6,this,event)">
<tr onclick="widgetListView_onClick('XXXX_list',7,this,event)">
<td class="listView_default_dataStyle" nowrap="" style="font-size:12px ;
font-family: sans-serif ;color: black ;background: #FFFFFF " 
ondblclick="XXXXListView_onDblClick('XXXXX_list',17, event)">NAME</td>
<td class="listView_default_dataStyle" nowrap="" style="font-size:12px ;font-family: sans-serif;
color: black ;background: #FFFFFF " ondblclick="XXXXX_onDblClick('XXXX_list',17, event)"> </td>
</tr>

Here is the code I am writing to try and find the user going by NAME in the table. 这是我正在编写的代码,试图在表中查找按NAME进行搜索的用户。

WebElement table = driver.findElement(By.id("table_list"));

    // Now get all the TR elements from the table
    List<WebElement> allRows = table.findElements(By.tagName("tr"));
    // And iterate over them, getting the cells
    for (WebElement row : allRows) {
     List<WebElement> cells = row.findElements(By.tagName("td"));
     for (WebElement cell : cells) {         
        List<WebElement> Names = cell.findElements(By.xpath("//td[text()='NAME']"));
        System.out.println(Names);

This just prints thousands of [] (the table is huge in the real application). 这仅打印成千上万的[](在实际应用程序中,该表非常大)。 Essentially what I need is to stop when I find the correct name and create a web element out of that table row. 本质上,我需要在找到正确名称并从该表行中创建Web元素时停止。 Which I can then click and drill down on. 然后,我可以单击并向下钻取。

Sorry if any of this is a bit vague, 抱歉,如果其中有些含糊,

Well if each name in the table is unique, you don't need to complicate things so much. 好吧,如果表中的每个名称都是唯一的,则无需使事情变得如此复杂。 Just search for element with text matching your 'Name' then select the row accordingly. 只需搜索文本与您的“姓名”匹配的元素,然后相应地选择行即可。 Look at the code below: 看下面的代码:

WebElement name = driver.findElement(By.xpath("//table[@id='XXXXXXXXX_list']//td[contains(text(),'NAME')]"));//Select td with text NAME in table with id XXXXXXXXX_list
WebElement rowWithName = name.findElement(By.xpath("./.."));//Select the parent node, i.e., tr, of the td with text NAME
/*
 * Look into that row for other element or perform any action on the row.
 */

If the names are not unique, ie, same name exists twice at similar node, 1st instance will be picked each time. 如果名称不是唯一的,即相同的名称在相似的节点上存在两次,则将每次选择第一个实例。 In that case we will have to try things differently, ie, we will have to index the xpath for correct instance of matching name. 在这种情况下,我们将不得不尝试不同的方法,即,我们将必须为xpath索引以找到匹配名称的正确实例。 Do ask if you have any further doubts :) 请问您是否还有其他疑问:)

This will help you out. 这将帮助您。

     try{
ArrayList<WebElement> cells = (ArrayList<WebElement>) driver.findElements(By.tagName("td"));
log4j.info("Value = "+input_type+" is stored in array from Webpage for "+keyword+" ");
for(WebElement type : cells)
{
    if(type.getAttribute("name").equals("your correct name here")) {

        type.sendKeys("ABC");

    }
}
return true;
}catch(Throwable e){
    return false;
}

You need to use Array list like this and you can compare your Name in which you wanna fill value Or wanna do any operation like getText(), click() etc. 您需要使用这样的数组列表,您可以比较要填充值的名称,也可以执行诸如getText(),click()等的任何操作。

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM