5

I'm looking to get the class attribute of every WebElement on the page quickly with selenium. Currently, I'm doing the following:

allElements = new ArrayList<WebElement>(m_webDriver.findElements(By.cssSelector("*")));

for (WebElement element : allElements) {
    String className = element.getAttribute("class");
}

This process is incredibly slow, taking upwards of thirty seconds on a page with 500 elements. I've tried parallelizing the getAttribute call, which is the slowest part of the method, but there was no speed increase. This leads me to believe that every call to getAttribute is fetching information instead of storing it locally.

Is there a faster or parallelizable way to do this?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
JordanKrissi
  • 55
  • 1
  • 6
  • What is it you are trying to accomplish? Doing something like this Selenium is going to be incredibly inefficient, as you have observed. – SiKing Jun 08 '15 at 18:45
  • I need a way to differentiate every element on the page (using a combination of class and location), so that I know if an element is added after a click or other interaction occurs – JordanKrissi Jun 08 '15 at 19:20

2 Answers2

5

The problem is, you cannot make selenium send batch getAttribute() calls for multiple elements. Here is a similar problem I've looked into - it is about making isDisplayed() work for multiple elements without making JSON Wire protocol requests for every element in a list:

But, as opposed to this isDisplayed() problem, here we can execute javascript and reliably get the class attribute values for every element on a page, something like this to get you started:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("var result = []; " +
"var all = document.getElementsByTagName('*'); " +
"for (var i=0, max=all.length; i < max; i++) { " +
"    result.push({'tag': all[i].tagName, 'class': all[i].getAttribute('class')}); " +
"} " +
" return result; ");
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

I would keep it simple and use an XPath instead of a complex snip of Javascript:

// get all elements where class attribute is not null
List<WebElement> allElements = m_webDriver.findElements(
  By.xpath(".//*[@class and not(ancestor::div[contains(@style,'display:none')])]")
);

for (WebElement element : allElements) {
    String className = element.getAttribute("class");
}
djangofan
  • 28,471
  • 61
  • 196
  • 289