我需要使用selenium web驱动程序选择表中的特定行,我有接近5000行,浏览每一行会很困难。如果我们有更好的方法吗?
答案 0 :(得分:1)
您需要点击一个复选框才能选择该行吗? 我会假设你的意思是: 我知道如何检查每一行,选择过滤器(即客户编号)的唯一方法,一旦找到它,单击选择复选框。 大多数表都是类似的,所以这里是一个例子:
= = = = = = =
=================================================================================================
= = = = = = =
=================================================================================================
= = = = = = =
=================================================================================================
Lets say that in the first row, is where the checkbox is, and that in the second row is the location of the filter.
meaning the data you will use in order to know that this is the row you need....
you will need to inspect the table element in order to find its ID, once you have that info you can use a similar method as the below:
Where you will use your webdriver, data filter, tableID and the number of the column where your data filter is located.
The method will return the row you need to click, then you can simply use the row[columnNumberLocation].Click() in order to select it.
public IWebElement returnRow(IWebDriver webDriver, string filter ,string tableName, int i)
{
IWebElement tableElement = webDriver.FindElement(By.Id(tableName));
IWebElement tbodyElement = tableElement.FindElement(By.TagName("TBODY"));
List<IWebElement> rows = new List<IWebElement>(tbodyElement.FindElements(By.TagName("tr")));
foreach (var row in rows)
{
List<IWebElement> cells = new List<IWebElement>(row.FindElements(By.TagName("td")));
if (cells.Count > 0)
{
string s = cells[i].Text;
if (s.Equals(filter))
{
return row;
}
}
}
return null;
}
答案 1 :(得分:1)
这取决于您选择该特定行的要求。
我假设你需要根据行索引选择它。 因此,所有关于定位元素的x路径都是如此。
例如:我需要获得行号2000
WebElement element = driver.findElement(By.xpath("//tr[2000]"));