gpt4 book ai didi

java - 线程 "main"org.openqa.selenium.WebDriverException : $ is not defined 中出现异常

转载 作者:行者123 更新时间:2023-12-01 12:35:48 26 4
gpt4 key购买 nike

我正在通过 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 时效果很好。

最佳答案

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

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

它可以在 Stack Overflow 上运行,因为 Stack Overflow 加载 jQuery。

关于java - 线程 "main"org.openqa.selenium.WebDriverException : $ is not defined 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25608227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com