Selenium Webdriver JQUERY [英] Selenium Webdriver JQUERY

查看:88
本文介绍了Selenium Webdriver JQUERY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Selenium WebDriver的新手,我正在学习Selenium WebDriver,了解如何使用jQuery选择器来处理元素而不是XPath表达式,ID等.

能否请您提供链接以帮助我找到有关如何在Selenium WebDriver中使用jQuery的基本信息?

解决方案

您不应该这样做. jQuery选择器提供CSS 2和CSS 3选择器的大部分功能,以及更多其他功能,但您可能可以不用它.如果您知道JQuery选择器,那么您已经知道CSS选择器.

在可能的地方使用CSS选择器,在不够的地方使用XPath表达式(它们更强大).我怀疑您会发现在这两种方法还不够的情况下会发现很多实际用法(然后,通常的方法是获取可以使用的方法并遍历集合,手动过滤结果).


也就是说,您也可以强制WebDriver接受JQuery选择器:

如果您只想支持一个或两个浏览器,最简单的方法可能是向该浏览器编写一个简单的插件,该插件会将JQuery插入每个页面(如果尚不存在).然后,您将强制该插件由正在使用的浏览器使用.

如果您想支持所有浏览器,那么该解决方案将很快成为负担,您最好的办法是编写WebDriver的装饰器,该装饰器将尝试在任何findElements()和/或<之前将JQuery注入到页面中. c1>调用(如果尚不存在).

请参见

如果需要,您可以轻松地将这些行包装到新的By对象或用于装饰的WebDriver的新的findElement(String jQuerySelector)方法中,但这取决于您的方便和懒惰,我们可以使它起作用...


我认为,最好的方法是创建一个名为ByJQuery的新By实现.有关如何制作ByJavaScript的信息,请参见
此答案-从此处进行重用仅一步之遥,将JQuery注入到页并运行实际查询.

class ByJQuery extends By implements Serializable {
    private final String query;

    public ByJQuery(String query) {
        checkNotNull(query, "Cannot find elements with a null JQuery expression.");
        this.query = query;
    }

    @Override
    public List<WebElement> findElements(SearchContext context) {
        WebDriver driver = getWebDriverFromSearchContext(context);

        if (!isJQueryInThisPage(driver)) {
            injectJQuery(driver);
        }

        return new ByJavaScript("return $(" + query + ")").findElements(context);
    }

    private static WebDriver getWebDriverFromSearchContext(SearchContext context) {
        if (context instanceof WebDriver) {
            return (WebDriver)context;
        }
        if (context instanceof WrapsDriver) {
            return ((WrapsDriver)context).getWrappedDriver();
        }
        throw new IllegalStateException("Can't access a WebDriver instance from the current search context.");
    }

    private static boolean isJQueryInThisPage(WebDriver driver) {
        // TODO Some JavaScript test for a JQuery object.
    }

    private static void injectJQuery(WebDriver driver) {
        // TODO Load JQuery from a file, inject it into the page via JS.
    }

    @Override
    public String toString() {
        return "By.jQuery: \"$(" + query + ")\"";
    }
}

I'm very new to Selenium WebDriver and I'm learning Selenium WebDriver on how to use jQuery selectors for working with elements instead of XPath expressions, IDs, etc...

Could you please help me on providing the link where I can find the basic information on how to use jQuery in the Selenium WebDriver?

解决方案

You shoudn't. JQuery selectors offer most of what CSS 2 and CSS 3 selectors do, plus something more, but you can probably live without it. If you know JQuery selectors, you already know CSS selectors.

Use CSS selectors where you can and use XPath expressions where it's not enough (they're stronger). I doubt you'll find many real usages where these two aren't enough (and then, the usual approach is to get what you can and iterate over the collection, filtering the results manually).


That said, you could possibly force WebDriver to accept JQuery selectors, too:

If you only want to support one or two browsers, the easiest way might be to write a simple addon to that browser which would inject JQuery to every page if it's not already present. You'd then force this addon to be used by the browser you're using.

If you want to support all of the browsers, that solution quickly becomes a burden and the best you could do is to write a decorator for WebDriver that would try to inject JQuery into the page before any findElements() and/or executeScript() call, if it's not already present.

See this question to get an idea about injecting JQuery.

After you've injected it, you can use it, again, only via JavaScript:

// earlier
if (driver instanceof JavascriptExecutor) {
    js = (JavascriptExecutor)driver;
} else {
    throw new IllegalStateException("This driver cannot run JavaScript.");
}

WebElement element = (WebElement)js.executeScript("$('div.account').get(0)");
// or
List<WebElement> elements = (List<WebElement>)js.executeScript("$('div.account').get()");

You can easily wrap those lines into a new By object, or a new findElement(String jQuerySelector) method for your decorated WebDriver, if you want, but that's up to your convenience and laziness, we got it working...


The best approach, I think, would be to create a new By implementation called ByJQuery. See this answer on how to make a ByJavaScript - it's a small step from there to reuse it, inject JQuery into the page and run the actual query.

class ByJQuery extends By implements Serializable {
    private final String query;

    public ByJQuery(String query) {
        checkNotNull(query, "Cannot find elements with a null JQuery expression.");
        this.query = query;
    }

    @Override
    public List<WebElement> findElements(SearchContext context) {
        WebDriver driver = getWebDriverFromSearchContext(context);

        if (!isJQueryInThisPage(driver)) {
            injectJQuery(driver);
        }

        return new ByJavaScript("return $(" + query + ")").findElements(context);
    }

    private static WebDriver getWebDriverFromSearchContext(SearchContext context) {
        if (context instanceof WebDriver) {
            return (WebDriver)context;
        }
        if (context instanceof WrapsDriver) {
            return ((WrapsDriver)context).getWrappedDriver();
        }
        throw new IllegalStateException("Can't access a WebDriver instance from the current search context.");
    }

    private static boolean isJQueryInThisPage(WebDriver driver) {
        // TODO Some JavaScript test for a JQuery object.
    }

    private static void injectJQuery(WebDriver driver) {
        // TODO Load JQuery from a file, inject it into the page via JS.
    }

    @Override
    public String toString() {
        return "By.jQuery: \"$(" + query + ")\"";
    }
}

这篇关于Selenium Webdriver JQUERY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆