简体   繁体   English

定位元素selenium + java的最快方法

[英]fastest way to locate elements selenium+java

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. 问题是硒似乎要花很长时间才能找到这些元素,并且它们被设置在计时器上,因此它们有时有时间在硒找到它们之前消失,我们正在谈论一个3秒的计时器。

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. 工具提示是使用javascript创建和销毁的,因此不会隐藏然后显示。 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. 尽管它似乎并没有更快地验证元素的存在。

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 这些是AutomationTest类的方法

    /**
     * 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 . 在性能方面,我不能对您这么强调。. 使用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> . 让我们找到<a>

CSS: CSS:

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

XPATH: XPATH:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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