Selenium WebDriver -在c#中获取表的第一行

本文关键字:一行 WebDriver 获取 Selenium | 更新日期: 2023-09-27 18:10:33

我是一个完全的selenium新手,所以下面的代码远非完美(它不工作的事实支持)。我想获得表中的第一行,并检查它是否有特定的CSS类分配给它。

我已经失败了,到目前为止,多次尝试后,只是得到第一行。这是我尝试过的-


我在这里使用(RemoteWebElement) cast的原因是因为(IWebElement) cast不工作。

IWebElement first_row = (RemoteWebElement)((IJavaScriptExecutor) Driver).ExecuteScript("return $('#headTable > tr:visible:first')");


下面一行获取所有可见行的文本。这工作。

IWebElement first_row = Driver.FindElement(By.CssSelector("#headTable"));


再次尝试获取第一行。

IWebElement first_row = Driver.FindElement(By.CssSelector("#headTable")).FindElement(By.CssSelector("tr:visible:first"));

我一直在看的例子http://docs.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example ..但它们都是非常简单的例子。

有人能看出这里出了什么问题吗?

另外,如果你有其他网站的链接,有c#的有用的例子,我将非常感激。谢谢。

Selenium WebDriver -在c#中获取表的第一行

实际上,BoltClock是对的,你使用jquery选择器到一个cssSelector

WebElement table = Driver.FindElement(By.CssSelector("#headTable")); // you have the table
list<WebElement> allElements = table.findElements(By.CssSelector("tr[attribute name='visible']")); // you have all row that are visible

,最后如果你想获得一行,执行allElements.get(0)

重要:

看看By.CssSelector("tr[attribute name ='visible']"),因为我只写了[attribute name='visible'],因为我不知道它是如何写进你的代码的,所以你可以适应。

关于cssSelector
有一些规范

任何时候你在WebDriver中使用HTML表,你应该考虑使用TableDriver扩展(https://github.com/jkindwall/TableDriver.NET)。对于查看表中的第一行是否具有特定CSS类的指定问题,您可以这样做:

Table table = Table.Create(driver.FindElement(By.Id("headTable")));
TableRow row = table.FindRow(0);
string rowClasses = row.Element.GetAttribute("class");
Assert.IsTrue(rowClasses.Contains("expected-class"));