java 是否可以使用 WebDriver 在网页上获取所有 WebElements

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12061169/
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-10-31 07:27:31  来源:igfitidea点击:

Is it possible to get ALL WebElements on a web page using WebDriver

javawebdriver

提问by kamal

Something like:

就像是:

WebElement inputs =  driver.findElement(By.tagName("input"));

or

或者

List<WebElement> links = driver.findElementsBy(By.tagName("a"));

What i wanted was a name/value pair like structure which gives me tagName vs Quantity My Objective is to have a list of these Elements and make a Series of tests associated with them, since different users would have their own unique set of these Elements per page. I hope i got my message across.

我想要的是一个像结构一样的名称/值对,它给我 tagName vs Quantity 我的目标是拥有这些元素的列表并进行一系列与它们相关的测试,因为不同的用户将拥有自己独特的这些元素集页。我希望我能传达我的信息。

回答by James

You could do something like:

你可以这样做:

List<WebElement> allElements = driver.findElements(By.xpath("//*"));

or

或者

List<WebElement> allElements = driver.findElements(By.cssSelector("*"));

Then just sort the list as needed using getTagName or other functions.

然后只需使用 getTagName 或其他函数根据需要对列表进行排序。