[英]Exception in thread “main” org.openqa.selenium.WebDriverException: $ is not defined
I am getting CSS selector by ID. 我正在通过ID获取CSS选择器。 I get an error when I call the getCSS
method as 当我调用getCSS
方法时出现错误
UpdateActualPath actualPath= new UpdateActualPath();
actualPath.getCSS("https://www.google.co.in/", "a");
Here is my code: 这是我的代码:
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;
}
}
The full error is: 完整的错误是:
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)
When I change the URL to http://stackoverflow.com it works fine. 当我将URL更改为http://stackoverflow.com时,它可以正常工作。
The problem you describe is consistent with jQuery not being present on the page you are loading. 您描述的问题与正在加载的页面上不存在jQuery一致。 You can avoid jQuery altogether by changing your first executeScript
call in getCSS
to: 您可以通过将getCSS
第一个executeScript
调用getCSS
为以下getCSS
来完全避免使用jQuery:
String strJavaScript =
(String) executor.executeScript("return document.body.innerHTML");
It works on Stack Overflow because Stack Overflow loads jQuery. 它适用于Stack Overflow,因为Stack Overflow会加载jQuery。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.