从网络表中获取行数 - Selenium WebDriver Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33233883/
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-08-11 14:29:43  来源:igfitidea点击:

Getting the count of rows from a web table - Selenium WebDriver Java

javaseleniumwebdriver

提问by Uziii

I am working on selenium webdriver using java. I am facing a problem. I have a web table and I have test case for add event functionality, that successfully adds the event in the table. Now I want to assert/verify if I got the added event in my table or not (Because if the functionality is broken, then date will not add in the table, and I can fail that test deliberately). The number of events are unknown before the start of test case. My logic for this test case verification is as follows:

我正在使用 java 开发 selenium webdriver。我正面临一个问题。我有一个 web 表,我有添加事件功能的测试用例,它成功地在表中添加了事件。现在我想断言/验证是否在我的表中添加了事件(因为如果功能被破坏,那么日期将不会添加到表中,并且我可以故意使该测试失败)。在测试用例开始之前,事件的数量是未知的。我对这个测试用例验证的逻辑如下:

  1. Add the event in the table. The code to add event is :

    public void addIndividualEvent(String eventDate, String eventName) throws Exception
    {
        driver.findElement(individualEventDate).sendKeys(eventDate);
        driver.findElement(individualEventName).sendKeys(eventName);
        driver.findElement(addIndividualEventBtn).click();
    }
    

    Now, after this step, there may be multiple events already in the table. So my next step would be to.

  2. Find the number of rows in the table.

  3. Construct a for loop that goes through each row and get the text of the date field.
  4. Add the result of for loop to a list.
  5. Then Assert.assertEquals(List,eventDate); will tell me if my added event is in the table or not.
  1. 在表中添加事件。添加事件的代码是:

    public void addIndividualEvent(String eventDate, String eventName) throws Exception
    {
        driver.findElement(individualEventDate).sendKeys(eventDate);
        driver.findElement(individualEventName).sendKeys(eventName);
        driver.findElement(addIndividualEventBtn).click();
    }
    

    现在,在这一步之后,表中可能已经有多个事件。所以我的下一步将是。

  2. 找出表中的行数。

  3. 构造一个遍历每一行的 for 循环并获取日期字段的文本。
  4. 将 for 循环的结果添加到列表中。
  5. 然后 Assert.assertEquals(List,eventDate); 会告诉我我添加的事件是否在表中。

Now, below is the HTML code of the table in my website.

现在,下面是我网站中表格的 HTML 代码。

<table class="table table-condensed table-hover event-list">
    <tbody>
        <tr class="study-event-row ng-scope" ng-repeat="event in events | orderBy:'-date'">
        <tr class="study-event-row ng-scope" ng-repeat="event in events | orderBy:'-date'">
        <tr class="study-event-row ng-scope" ng-repeat="event in events | orderBy:'-date'">
    </tbody>
</table>

I was able to construct the xpath that highlights all my rows available in the table.

我能够构建突出显示表中所有可用行的 xpath。

xpath = //table[@class='table table-condensed table-hover event-list']/tbody/tr

Now the issue I am facing is that I dont know how to use this xpath to get the count of rows from my table. I tried to use xpath.getSize();but this returned the dimensions of the table and not the actual count.

现在我面临的问题是我不知道如何使用这个 xpath 从我的表中获取行数。我尝试使用,xpath.getSize();但这返回了表格的尺寸而不是实际计数。

Can anyone tell me how can I get the no of rows? Thanks

谁能告诉我如何获得行数?谢谢

回答by gtlambert

You could use the XPath:

您可以使用 XPath:

count(//table[@class='table table-condensed table-hover event-list']//tr)

This will count all the trelements that are descendants of the tableelement with @class="table table-condensed table-hover event-list"

这将计算作为tr元素后代的所有table元素@class="table table-condensed table-hover event-list"

回答by Jeremiah

Resolve the table object through your xpath, then use that WebElement as your SearchContext and get all TR elements within. The size of the return list should tell you your visiblerow count.

通过 xpath 解析 table 对象,然后使用该 WebElement 作为 SearchContext 并获取其中的所有 TR 元素。返回列表的大小应该告诉您可见的行数。

int EXPECTED_ROW_COUNT = 3; 

/**
 * Test case for counting the number of visible rows in a table.
 * @see "http://stackoverflow.com/questions/33233883/getting-the-count-of-rows-from-a-web-table-selenium-webdriver-java"
 *  @author http://stackoverflow.com/users/5407189/jeremiah
 */
@Test
public void testCountRows() {
    WebDriver wd = new FirefoxDriver();
    wd.get("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_table");
    WebDriverWait wait = new WebDriverWait(wd, 10);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframeResult"));//Specific to test location @ w3schools!
    WebElement parentTable = wd.findElement(By.xpath("html/body/table/tbody")); 
    //WebElement parentTable = wd.findElement(By.xpath("//table[@class='table table-condensed table-hover event-list']/tbody"));


    //Now use the parentTable as the root of the search for all 'tr' children.
    List<WebElement> rows = parentTable.findElements(By.xpath("./tr"));
    Assert.assertTrue(EXPECTED_ROW_COUNT == rows.size());
    }

   /**
     * Test that you can use to direct at your url and interact with rows.
     */
    @Test
    public void testYourRowCount() {
        WebDriver wd = new FirefoxDriver();
        //FIXME
        wd.get("yourURLHere");
        WebElement parentTable = wd.findElement(By.xpath("//table[@class='table table-condensed table-hover event-list']/tbody"));
        //Now use the parentTable as the root of the search for all 'tr' children.
        List<WebElement> rows = parentTable.findElements(By.xpath("./tr"));
        //FIXME:  Do stuff with the rows.        
    }

回答by JeffC

Assuming your XPath works, you can just do this...

假设你的 XPath 工作,你可以这样做......

List<WebElement> rows = driver.findElements(By.xpath("//table[@class='table table-condensed table-hover event-list']/tbody/tr"));
System.out.println(rows.size());

It will return you the number of rows (TRs) in that table.

它将返回TR该表中的行数(s)。

回答by AGill

To get the rows, do the following- Using CSS selector:

要获取行,请执行以下操作 - 使用 CSS 选择器:

List<WebElement> rows = driver.findElement(By.cssSelector("[class='table table-condensed table-hover event-list'] tr"));

Or if you have to use XPATH, then do:

或者,如果您必须使用 XPATH,请执行以下操作:

List<WebElement> rows = driver.findElements(By.xpath("//table[@class='table table-condensed table-hover event-list']/tbody/tr"));

This is returning a list of WebElements. You can get the count here:

这将返回一个 WebElements 列表。你可以在这里得到计数:

int count = rows.size();
System.out.println("ROW COUNT : "+count);

Then you can get text for each element and assert if it's as expected. As you are saying the text should be same for all elements, then you can do something like this:

然后您可以获得每个元素的文本并断言它是否符合预期。正如您所说,所有元素的文本都应该相同,那么您可以执行以下操作:

for(WebElement e : rows) {
        assertEquals("expected text", e.getText());
    }