【问题标题】:Exception in thread "main" org.openqa.selenium.WebDriverException: $ is not defined线程“主”org.openqa.selenium.WebDriverException 中的异常:$ 未定义
【发布时间】:2014-10-25 19:46:06
【问题描述】:

我正在通过 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)

当我将 URL 更改为 http://stackoverflow.com 时,它可以正常工作。

【问题讨论】:

    标签: java jquery selenium jquery-selectors


    【解决方案1】:

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

    String strJavaScript = 
        (String) executor.executeScript("return document.body.innerHTML");
    

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

    【讨论】:

      猜你喜欢
      • 2018-12-31
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2023-01-24
      • 1970-01-01
      相关资源
      最近更新 更多