简体   繁体   中英

Is there a faster method to get elements than the findElement method in Selenium?

I am currently writing some automated tests using Selenium in Java to test a search engine and its results in a webpage, but I have noticed that the findElement calls tend to run pretty slow. I use this call a lot as I iterate through the displayed results to make sure that they are displaying properly (As well as a few other tests).

I want to know if there is a faster method I could use, or if there are other recommendations out there for speeding up this process.

Example calls:

   driver.findElement(By.xpath("//section[@id='results']/ul/li[1]/a/h1")).click(); // choose first result
   driver.findElement(By.className("total"));

I am wondering if perhaps the By.xpath function is slower than the other functions such as the By.className function. I'm relatively new to automated testing, but I feel like the tests I am running shouldn't be as slow as they are. Any help is appreciated. Thanks!

I ended up using the findElements() function and iterating through the generated list. A lot of the repetitive tasks I needed to perform was the check the formatting of search results that were being kept in lists in the HTML. The findElements() was useful because it only needed to go to the webpage itself one (with its initial call) and then all the iterations and operations I performed on it could be executed in memory.

The findElements() was also useful for checking for a lack of elements, per the Selenium documentation at https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html . After making the changes to my tests, I saw an average speedup of about %30 (though for some of the smaller tests, the changes were almost negligible...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM