Selenium - How to capture all web page elements and associated locators on a page?

三世轮回 提交于 2019-12-04 12:57:59
Jadent

Using findElementsByXPath and specifying 'All elements' worked for me. E.g.

findElementsByXPath("//*")

http://www.w3schools.com/xml/xpath_syntax.asp

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.

Ashish Mathur

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

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 );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!