线程“main”中的异常org.openqa.selenium.WebDriverException:$未定义

时间:2014-09-01 14:40:56

标签: java jquery selenium jquery-selectors

我正在通过ID获取CSS选择器。当我将getCSS方法称为

时,出现错误
UpdateActualPath actualPath= new UpdateActualPath();
    actualPath.getCSS("https://www.google.co.in/", "a");

这是我的代码:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UpdateActualPath {
    static UpdateActualPath updateObject = new UpdateActualPath();
    static WebDriver driver = new FirefoxDriver();

    public void getCSS(String url, String tagname) throws IOException {
        // driver.get("http://scripting.jdpoweronline.com/mrIWeb/mrIWeb.dll?I.Project=T1_QTYPE&i.test=1");
        driver.get("file:///home/himansu/Desktop/Static.html#4"); // url
        String jQuerySelector = "'body'";
        JavascriptExecutor executor = (JavascriptExecutor) driver;
        String strJavaScript = (String) executor.executeScript("return $(" + jQuerySelector + ").html()");
        Document docparse = Jsoup.parse(strJavaScript);
        Elements inputTags = docparse.select("a"); // tagname
        if (!inputTags.isEmpty()) {
            Element tempTag = null;
            for (Element inputTag : inputTags) {
                String tempString = "";
                tempTag = inputTag;

                while (tempTag.tagName() != "html") {
                    String tagId = "";
                    String tagClass = "";
                    String tagType = "";
                    String tagLink = "";
                    if (tempTag.id() != "") {
                        tagId = "#" + tempTag.id() + " ";
                        tempString = tempTag.tagName() + tagId + tempString;
                        break;
                    }

                    else {
                        System.out.println("This Type Tag is Not Present in Current Web Page.....!!!!");
                    }
                }
            }
        }
    }

    private String findHrefAtttribute(Element tempTag, Elements tags) {
        String tlink = tempTag.attr("href");
        String css = "[href='" + tlink + "']";
        if (updateObject.checkType(tlink, tags) == 1)
            return css;
        else
            return "";

    }

    public String findChekboxAtttribute(Element tag, Elements tags) {
        String tagname = tag.attr("name");
        String css = "[name='" + tagname + "']";
        if (updateObject.checkType(tagname, tags) == 1)
            return css;
        else
            return "";
    }

    public String findImageAtttribute(Element tag, Elements tags) {
        String tagalt = tag.attr("alt");
        String tagname = tag.attr("name");
        if (updateObject.checkType(tagname, tags) == 1) {
            String css = "[alt='" + tagalt + "']" + "[name='" + tagname + "']";
            return css;
        } else {
            String css = "[alt='" + tagalt + "']";
            return css;
        }
    }

    public int checkType(String tagname, Elements chektags) {
        int count = 0;
        for (Element matchInputTag : chektags) {
            String matchtaghreftype = matchInputTag.attr("href");
            String matchtagtype = matchInputTag.attr("name");
            if (matchtagtype.compareTo(tagname) == 0)
                count++;
            if (matchtaghreftype.compareTo(tagname) == 0) {
                count++;
            }
        }
        return count;
    }

}

完整错误是:

 Exception in thread "main" org.openqa.selenium.WebDriverException: $ is not defined
    Command duration or timeout: 513 milliseconds
    Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
    System info: host: 'ATMECSINDT-068', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.8.0-37-generic', java.version: '1.7.0_55'
    Session ID: f706c2df-360d-4970-9ea7-9aa856ab32de
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=30.0}]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
        at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:504)
        at publicc.UpdateActualPath.getCSS(UpdateActualPath.java:25)
        at publicc.Test.main(Test.java:29)
    Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: $ is not defined
    Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
    System info: host: 'ATMECSINDT-068', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.8.0-37-generic', java.version: '1.7.0_55'
    Driver info: driver.version: unknown
        at <anonymous class>.anonymous(https://www.google.co.in/ line 68 > Function:1:1)
        at <anonymous class>.handleEvaluateEvent(https://www.google.co.in/:68:1)

当我将网址更改为http://stackoverflow.com时,工作正常。

1 个答案:

答案 0 :(得分:1)

您描述的问题与您正在加载的页面上不存在jQuery一致。您可以通过将executeScript中的第一个getCSS电话更改为:

来完全避免使用jQuery
String strJavaScript = 
    (String) executor.executeScript("return document.body.innerHTML");

它适用于Stack Overflow,因为Stack Overflow加载了jQuery。