简体   繁体   English

如何处理 selenium c# 中的 web 表

[英]How to handle web table in selenium c#

In my application, i have to assert name on the webtable but i am unable to do so.在我的应用程序中,我必须在 webtable 上声明名称,但我无法这样做。

Below is the image of my HTML code and i am unable to copy paste here.下面是我的 HTML 代码的图像,我无法在此处复制粘贴。

在此处输入图像描述

I have to get the name:"Robert Brits" from the web table and want to assert the same.我必须从 web 表中获得名称:“Robert Brits”,并且想要断言相同。

I tried the below logic:我尝试了以下逻辑:

 IWebElement tableElement = driver.FindElement(By.Xpath("//table[@id='globalSearchTable']"));
            IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
            IList<IWebElement> rowTD;
            foreach (IWebElement row in tableRow)
            {
                rowTD = row.FindElements(By.TagName("td"));
                {
                  _____
                  }
             }

I'm not sure how to proceed further and complete the code.我不确定如何进一步完成代码。 Could anyone help me?谁能帮帮我? Thanks in advance提前致谢

You could do something like this....你可以做这样的事情......

    var value = "Robert Brits";

    var table = driver.FindElement(By.Xpath("//table[@id='globalSearchTable']")
    foreach (var tr in table.FindElements(By.TagName("tr")))
    {
        var tds = tr.FindElements(By.TagName("td"));
        for (var i = 0; i < tds.Count; i++)
        {
           if (tds[i].Text.Trim().Contains(value))
         {
               var test = tds[i].Text.Trim();
                Assert.IsTrue(test.Contains(value));
                break;
         }

        }
    }

I know this looks odd with the conditional statement but I left it there for a reason.我知道这与条件语句看起来很奇怪,但我把它留在那里是有原因的。 Most tables have many columns.大多数表都有很多列。 So you may want the if statement to look for a name, account number, email etc. and stop.因此,您可能希望 if 语句查找名称、帐号、email 等并停止。 Then you may need to go to the left or right in that row to get text or click a link.然后您可能需要在该行的左侧或右侧 go 以获取文本或单击链接。 In this example you could call tds[i] as the search criteria and then use tds[i-2] to go two columns to the left of tds[i] or use tds[i+3] to go three columns to the right of tds[i].在此示例中,您可以调用 tds[i] 作为搜索条件,然后使用 tds[i-2] 到 go tds[i] 左侧的两列或使用 tds[i+3] 到 go 右侧的三列的 tds[i]。

If you want to only verify that value try following xpath .如果您只想验证该值,请尝试遵循xpath

IWebElement tableElement = driver.FindElement(By.Xpath("//table[@id='globalSearchTable']/tbody/tr[1]/td[3]"));
Assert.AreEqual(tableElement.Text,"Robert Brits")

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

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