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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:22:54  来源:igfitidea点击:

Selenium - Find all elements of a web page

javaseleniumfindwebpageelements

提问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);
            }