线程"main"中的异常org.openqa.selenium.WebDriverException:未定义$ [英] Exception in thread "main" org.openqa.selenium.WebDriverException: $ is not defined

查看:90
本文介绍了线程"main"中的异常org.openqa.selenium.WebDriverException:未定义$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过ID获取CSS选择器.调用getCSS方法为

I am getting CSS selector by ID. I get an error when I call the getCSS method as

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 时,它工作正常.

When I change the URL to http://stackoverflow.com it works fine.

推荐答案

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

The problem you describe is consistent with jQuery not being present on the page you are loading. You can avoid jQuery altogether by changing your first executeScript call in getCSS to:

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

它可用于Stack Overflow,因为Stack Overflow会加载jQuery.

It works on Stack Overflow because Stack Overflow loads jQuery.

这篇关于线程"main"中的异常org.openqa.selenium.WebDriverException:未定义$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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