问题
What Java/Selenium commands can I use to capture/get/output for all elements and associated element locators for a single webpage? The Selenium IDE allows you to inspect one element at a time. That is a problem if you have thousands of elements to automate. Is there a tool or Java/selenium command that I can use to get all of the objects/elements on my web page at once and then maybe customize the output to suite my needs? If you have any experience with SilkTest, I'd like something analogous to generating Window Declarations in SilkTest. SilkTest's Record Window Declaration tool captures tag/property/locator information for all of the objects/elements on a page and allows you to paste the code to your library or include file. So with one or two clicks I can capture and define dozens of objects in SilkTest. Is there a tool or command that does something similar for Selenium? I'm using Java so I'd like any examples in Java. Thanks.
回答1:
Using findElementsByXPath and specifying 'All elements' worked for me. E.g.
findElementsByXPath("//*")
http://www.w3schools.com/xml/xpath_syntax.asp
回答2:
You might not find any "feature" that comes along with selenium to do this. You will have to use some tools like
Firebug, Webdeveloper
(both are firefox extensions) to find the xpath/css path of locators which you need. Also, why would you need to get "all" the elements? Chances of using "all" elements in a page might be very less I feel. Easiest way (which I think you are already doing) is to use IDE to create a prototype of your test case and then use Firebug or similar tools to optimize the xpath.
回答3:
There is a chrome extension - Selenium Page Object Generator- which generates a .java class having all the locators of the page for page object model . It's pretty Cool. Check it out!!
https://chrome.google.com/webstore/detail/selenium-page-object-gene/epgmnmcjdhapiojbohkkemlfkegmbebb
回答4:
List<WebElement> el = driver.findElements(By.xpath("//*"));
int count=0;
for ( WebElement e : el ) {
System.out.println( e.getTagName()+" "+e.getText());
count++;
}
System.out.println(count );
来源:https://stackoverflow.com/questions/6754284/selenium-how-to-capture-all-web-page-elements-and-associated-locators-on-a-pag