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

标签 c# selenium xpath

DataList

从上图中,我添加的新数据被添加到最后一页,但可能有相似的名称,我需要使用所示的 ID 来验证数据。因此,我试图找出存储 id 中的文本值的方法,并且当添加新数据时,它应该验证最后一个新添加的 ID。有任何想法吗?

        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 (??)
            }

最佳答案

正如@viet-pham所说,使用last是个好主意,你也可以使用相对xpath,例如:

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

并且您不需要使用 [1],因为您使用的是 FindElement 而不是 FindElements 并且返回找到的第一个元素.

关于c# - 使用动态列表c#验证xpath最后一个元素的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711586/

相关文章:

java - 在测试用例中使用 if/switch/for 条件

xml - xslt 转换中表达式 '[' 中的意外标记

c# - DateTime.Now 忽略 Windows 10 设置

c# - 简单喷油器跟踪时间

C# Magick.Net "no decode delegate for this image format"与 SVG

java - 在 eclipse 中创建了一个 java 测试脚本,如何在 headless Linux CentOS VM 中运行它?

c# - 请求方法和线程问题?

python - Selenium 不会在新标签页中打开新 URL(Python 和 Chrome)

xml - XPath/XSLT : how to select all elements that satisfy a condition involving another set of elements

java - Selenium 点击浏览器的 url 而不是网站的搜索框