Java Selenium - 查找网页的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19683016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium - Find all elements of a web page
提问by AryA
I am planning a tool in Java which would have a drop down containing all the elements of a web page. Is there any way I can read those into a data structure?
我正在计划一个 Java 工具,它会有一个包含网页所有元素的下拉菜单。有什么办法可以将它们读入数据结构中吗?
回答by ddavison
Yes, there is a way.
是的,有办法。
Here is some pseudo-code:
下面是一些伪代码:
List<WebElement> el = driver.findElements(By.cssSelector("*"));
for ( WebElement e : el ) {
add(e.tagName());
}
回答by chetstriker
non-pseudo C# version of above: (although I'm just displaying the results in a console
上面的非伪 C# 版本:(虽然我只是在控制台中显示结果
IReadOnlyCollection el = driver.FindElements(By.CssSelector("*"));
IReadOnlyCollection el = driver.FindElements(By.CssSelector("*"));
foreach (IWebElement elm in el)
{
Console.WriteLine(elm.TagName + elm.Text);
}