fastest way to locate elements selenium+java

我与影子孤独终老i 提交于 2019-12-13 17:46:36

问题


I need to check my site for invalid tooltips and other error mesages on my site during the tests to make sure everything is okey. the problem is that selenium seem to take forever to find these elements and they are set on a timer so they sometimes have time to disapere before selenium finds them, we are talking about a 3 second timer.

right now I find them like so:

      List<WebElement> error = driver.findElements(By.cssSelector("div.alert .error"));
        if (error.size() == 0) {return true;}
        else {return false;}

this will find all the error messages that are shown on the site at the given time and this is sometimes to slow to keep up. the tooltips are created and destroyd with javascript so they are not hidden and then displayed. Any thoughts on this?

EDIT:

Since the site is single page it's a massive amount of elements to go through to find the right one, altho it's fast when I look for a item that is actually vissable so to minimize my search I did like this:

WebElement messages = driver.findElement(By.id("the-div-the-message-is-created-in"));
List<WebElement> error = messages.findElements(By.cssSelector("div.alert .error"));
if (error.size() == 0) {return true;}
else {return false;}

altho it didn't seem to verify the existence of the element it any faster.


回答1:


Something that I put in every single framework i've ever built, which is VERY efficient.. Here is an exerpt from the framework found here. You can download it here...

I implement sort of a pseudo-wait type method before i ever perform any actions on objects. Try it yourself. It's very efficient.

These are methods from the AutomationTest class

    /**
     * Checks if the element is present or not.<br>
     * @param by
     * @return <i>this method is not meant to be used fluently.</i><br><br.
     * Returns <code>true</code> if the element is present. and <code>false</code> if it's not.
     */
    public boolean isPresent(By by) {
        if (driver.findElements(by).size() > 0) return true;
        return false;
    }

    /**
     * Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
     */
    private WebElement waitForElement(By by) {
        int attempts = 0;
        int size = driver.findElements(by).size();

        while (size == 0) {
            size = driver.findElements(by).size();
            if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
                                                             by.toString(),
                                                             MAX_ATTEMPTS));
            attempts++;
            try {
                Thread.sleep(1000); // sleep for 1 second.
            } catch (Exception x) {
                fail("Failed due to an exception during Thread.sleep!");
                x.printStackTrace();
            }
        }

        if (size > 0) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");

        return driver.findElement(by);
    }

Combine these methods yourself, and you're good to go.

ALSO

In terms of performance, I cannot stress to you this enough.. USE CSS. It is faster, and cleaner. See for yourself.

Consider the following,

<div id="something">
  <div class="someClass">
    <a href='http://google.com/search?'>Search Google</a>
  </div>
</div>

Let's find the <a>.

CSS:

div#something div.someClass > a[href^='http://google']

XPATH:

//div[@id='something']/div[contains(@class, 'someClass')]/a[starts-with(@href, 'http://google')]


来源:https://stackoverflow.com/questions/19255752/fastest-way-to-locate-elements-seleniumjava

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