从表中选择行 - Java Selenium

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38060054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:04:20  来源:igfitidea点击:

Selecting row from Table - Java Selenium

javaseleniumhtml-tablerow

提问by LearningJS888

I have a quick Java table-question. I have a table, containing 4 columns, and X rows. The column's are as follows: Quote Number, Name, Date, and an Add button. I'm trying to, after looking for a specific name in the table, click on the add-button on the row the name was found.

我有一个快速的 Java 表格问题。我有一个表,包含 4 列和 X 行。该列如下:报价编号、名称、日期和添加按钮。我正在尝试在表中查找特定名称后,单击找到名称的行上的添加按钮。

Example:

例子:

Quote#  Name       Date        Add-Button

100001  John Doe1  01/01/1980  [Add]
100002  John Doe2  01/01/1981  [Add]
100003  John Doe3  01/01/1982  [Add]
100004  John Doe4  01/01/1983  [Add]

I've written the following code, to identify if a certain name is in the table:

我编写了以下代码,以识别表中是否包含某个名称:

public List<String> getInternetQuoteNames()
    {
        List<WebElement> webName = sh.getElements(newInternetQuotesNames);
        List<String> stringName = new ArrayList<>();
        for(WebElement name : webName)
            stringName.add(name.getText());
        return stringName;
    }

And am now trying to write another method that clicks on the correct [Add]-button, but I'm not sure how to access the correct row as I'm not good with tables. What I wrote so far is:

现在我正在尝试编写另一种方法来点击正确的 [添加] 按钮,但我不确定如何访问正确的行,因为我不擅长表格。到目前为止我写的是:

public void addInternetQuoteByName(String name)
    {
        List<String> listOfNames = getInternetQuoteNames();
        if(listOfNames.contains(name))
        {
            // Find row of provided name
           // Click on [Add]-button on that row
        }
    }

The HTML code for the Table is as follows:

表格的 HTML 代码如下:

<tbody>
    <tr>
        <td>100040365822</td>
        <td>Test06232016 Test033929</td>
        <td>06/23/2016</td>
        <td>
            <a id="assignQuoteLink_100040365822" class="add_from_pqs" href="#">Add Quote</a>
        </td>
    </tr>
    <tr class="alt">
        <td>100040365772</td>
        <td>Test06232016 Test033723</td>
        <td>06/23/2016</td>
        <td>
            <a id="assignQuoteLink_100040365772" class="add_from_pqs" href="#">Add Quote</a>
        </td>
    </tr>
    <tr>
        <tr class="alt">
            <tr>
                <tr class="alt">
</tbody>

Any tips on how to complete the method and click on the right row's [Add]-button is greatly appreciated!

非常感谢有关如何完成该方法并单击右行的 [添加] 按钮的任何提示!

回答by Amit

Give a try to this approach:

试试这个方法:

  1. Get the size of the table driver.findElements(By.xpath("table xpath")).size()
  2. Iterate over each name record in table and find if the match is found as passed in the input.
  3. Click the add button.
  1. 获取表的大小 driver.findElements(By.xpath("table xpath")).size()
  2. 遍历表中的每个名称记录并查找是否在输入中找到匹配项。
  3. 单击添加按钮。

Code

代码

public static void addInternetQuoteByName(String Name) 
{
    WebDriver driver = new FirefoxDriver();
    int rowCount = driver.findElements(By.xpath("xpath of table")).size();

    for (int i = 1; i <= rowCount; i++) 
    {
        String sCellValue = driver.findElement(By.xpath("XPATH Of Name row")).getText();

        if (sCellValue.equalsIgnoreCase(Name)) 
        {
             driver.findElement(By.xpath("xpath of add")).click();
        }
    }

    driver.close();
}

xpath for name and add button have to passed as making the tr[i]dynamic for eg: .//*[@id='content']/table/tbody/tr["+i+"]/td[2]

名称和添加按钮的 xpath 必须作为使 tr[i]dynamic 传递,例如: .//*[@id='content']/table/tbody/tr["+i+"]/td[2]

As I was not sure of exact xpath for name and add button column I have provided an example.

由于我不确定名称和添加按钮列的确切 xpath,因此我提供了一个示例。

Let me know if you have issues.

如果您有问题,请告诉我。

回答by JeffC

Here's a function to does what you requested. It grabs all the TRs, looks in the 2nd cell (TD) for the name and if found, clicks the "Add Quote" link for that row.

这是一个执行您要求的功能。它抓取所有 TR,在第二个单元格 (TD) 中查找名称,如果找到,则单击该行的“添加引用”链接。

public void addByName(String name)
{
    List<WebElement> rows = driver.findElements(By.tagName("tr"));
    for (WebElement row : rows)
    {
        List<WebElement> cells = row.findElements(By.tagName("td"));
        if (cells.get(1).getText().equals(name))
        {
            cells.get(3).findElement(By.linkText("Add Quote")).click();
            return;
        }
    }
}

回答by vins

This requirement sounds a lot like this article. http://www.testautomationguru.com/selenium-webdriver-finding-webelements-using-map/

这个要求听起来很像这篇文章。http://www.testautomationguru.com/selenium-webdriver-finding-webelements-using-map/



Basically, you need insert some javascript to create structure as shown here.

基本上,您需要插入一些 javascript 来创建如下所示的结构。

{
    "John Doe1": {
        "Add" : []
    },
    "John Doe2": {
        "Add" : []
    }   

}

then get the results returned and type caste to a Map.

然后获取返回的结果并将 caste 输入到 Map 中。

Map < String, WebElement >> nameMap = ((JavascriptExecutor) driver).executeScript(queryString);

then your usage would be like

那么你的用法就像

nameMap.get("John Doe3").get("Add").click();