简体   繁体   English

使用动态列表c#验证来自xpath最后一个元素的数据

[英]Verify the data from the last element of xpath with dynamic list c#

DataList控件

From the above image the new data that I add gets added to the last page but there can be similar name and I need to verify the data using the ID as shown. 从上面的图像中,我添加的新数据被添加到最后一页,但是名称可能相似,我需要使用所示的ID来验证数据。 So I am trying to figure out the way to store text values from the id and when the new data is added it should verify the last newly added ID. 因此,我试图找出从ID存储文本值的方法,并且在添加新数据时,它应该验证最后一个新添加的ID。 Any ideas? 有任何想法吗?

        int i = 1;
        bool found = false;
        string ID;
        try
        {

            IWebElement LastPage = Driver.driver.FindElement(By.XPath("html/body/div[4]/div/div/div[2]/table/tbody/tr[8]/td[1]"));
            LastPage.Click();
            for (i = 1; i <= 10; i++) ;
            {
                ID= Driver.driver.FindElement(By.XPath("html/body/div[4]/div/div/div[2]/table/tbody/tr[" + i + "]/td[1]")).Text;
                if (??)
            }

As @viet-pham said, using last is a good idea, you can also use a relative xpath like: 正如@ viet-pham所说,使用last是个好主意,您也可以使用相对xpath:

//table/tbody/tr[last()-1]/td

and you don't need to use [1] since you are using FindElement and not FindElements and is returning the first element found. 并且您不需要使用[1]因为您使用的是FindElement而不是FindElements ,并且返回找到的第一个元素。

You don't need to use for loop to get the last item, just put last() to select the final tr element 您不需要使用for循环即可获取最后一个项目,只需将last()放入以选择最后一个tr元素

In your case : 在您的情况下

IWebElement lastElement = Driver.driver.FindElement(By.XPath("(html/body/div[4]/div/div/div[2]/table/tbody/tr)[last()]/td[1]"));
ID = lastElement.Text;

In additional: because the last row is the total, so you must change to the row before it => [last()-1] 另外:由于最后一行是总数,因此必须更改为它之前的行=> [last()-1]

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

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