简体   繁体   English

org.openqa.selenium.JavascriptException:javascript 错误:$ 未定义错误,使用 ChromeDriver Selenium 使用 Ashot 截取坐标截图

[英]org.openqa.selenium.JavascriptException: javascript error: $ is not defined error taking coordinate screenshot with Ashot using ChromeDriver Selenium

I have encountered a weird problem while working with AShot .我在使用AShot时遇到了一个奇怪的问题。 Ashot works fine for whole screenShots but while selective screenShot it is BAD . Ashot适用于整个 screenShots 但选择性 screenShot 它是BAD It throws error in CoordsProvider Class while getting co-ordinates of element.它在获取元素坐标时在CoordsProvider类中引发错误。 am I using a faulty build or product?我使用的是有问题的版本还是产品?

public abstract class CoordsProvider implements Serializable {

public abstract Coords ofElement(WebDriver driver, WebElement element);

public Set<Coords> ofElements(WebDriver driver, Iterable<WebElement> elements) {
    Set<Coords> elementsCoords = new HashSet<>();
    for (WebElement element : elements) {
        ***Coords elementCoords = ofElement(driver, element);  //fails here***
        if (!elementCoords.isEmpty()) {
            elementsCoords.add(elementCoords);
        }
    }
    return Collections.unmodifiableSet(elementsCoords);
}

@SuppressWarnings("UnusedDeclaration")
public Set<Coords> ofElements(WebDriver driver, WebElement... elements) {
    return ofElements(driver, Arrays.asList(elements));
}

@SuppressWarnings("UnusedDeclaration")
public Set<Coords> locatedBy(WebDriver driver, By locator) {
    return ofElements(driver, driver.findElements(locator));
}
}

在此处输入图片说明 Stack trace堆栈跟踪

org.openqa.selenium.JavascriptException: javascript error: $ is not defined
  (Session info: chrome=79.0.3945.130)
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'OPTIMIZEQ-LTP03', ip: '192.168.99.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.2'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 78.0.3904.105 (60e2d8774a81..., userDataDir: C:\Users\SHAILE~1.SIN\AppDa...}, goog:chromeOptions: {debuggerAddress: localhost:61006}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(manual, http=localhos..., setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webdriver.remote.sessionid: f5a47b27537f019dacb73462732...}
Session ID: f5a47b27537f019dacb734627324a790
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) ~[selenium-remote-driver-3.14.0.jar:na]
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) ~[selenium-remote-driver-3.14.0.jar:na]
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) ~[selenium-remote-driver-3.14.0.jar:na]
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) ~[selenium-remote-driver-3.14.0.jar:na]
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548) ~[selenium-remote-driver-3.14.0.jar:na]
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:485) ~[selenium-remote-driver-3.14.0.jar:na]
    at ru.yandex.qatools.ashot.util.JsCoords.findCoordsWithJquery(JsCoords.java:30) ~[ashot-1.5.2.jar:na]
    at ru.yandex.qatools.ashot.coordinates.JqueryCoordsProvider.ofElement(JqueryCoordsProvider.java:13) ~[ashot-1.5.2.jar:na]
    at ru.yandex.qatools.ashot.coordinates.CoordsProvider.ofElements(CoordsProvider.java:21) ~[ashot-1.5.2.jar:na]
    at ru.yandex.qatools.ashot.AShot.takeScreenshot(AShot.java:115) ~[ashot-1.5.2.jar:na]
    at ru.yandex.qatools.ashot.AShot.takeScreenshot(AShot.java:132) ~[ashot-1.5.2.jar:na]
    at com.optq.main.util.SeleniumDriverUtility.captureAShotElement(SeleniumDriverUtility.java:563) ~[classes/:na]

Any help or Alternatives For Ashot are welcome,Please help me into this.欢迎任何帮助或 Ashot 的替代品,请帮助我解决这个问题。

This error message...这个错误信息...

org.openqa.selenium.JavascriptException: javascript error: $ is not defined
  (Session info: chrome=79.0.3945.130)
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'OPTIMIZEQ-LTP03', ip: '192.168.99.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.2'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 78.0.3904.105 (60e2d8774a81..., userDataDir: C:\Users\SHAILE~1.SIN\AppDa...}, goog:chromeOptions: {debuggerAddress: localhost:61006}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(manual, http=localhos..., setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webdriver.remote.sessionid: f5a47b27537f019dacb73462732...}

...implies that the ChromeDriver was unable to interact with the Browsing Context ie Chrome Browser session. ...暗示ChromeDriver无法与浏览上下文(Chrome 浏览器会话)进行交互。


Deep dive深潜

As per the documentation in ReferenceError: "x" is not defined this error means there is a non-existent variable referenced somewhere within the DOM Tree .根据ReferenceError 中的文档: "x" is not defined这个错误意味着在DOM Tree 的某处引用了一个不存在的变量。 This variable needs to be declared, or you need to make sure it is available in your current script or scope.这个变量需要声明,或者你需要确保它在你当前的脚本或作用域中可用。

Hint : When loading a library (such as jQuery), make sure it is loaded before you access library variables, such as "$".提示:加载库(例如jQuery)时,请确保在访问库变量(例如“$”)之前加载它。 Put the tag that loads the library before your code that uses it.将加载库的标记放在使用它的代码之前。

As per the discussion JavaScript/jQuery - “$ is not defined- $function()” error @Ketan mentions that this error occurs when you have not made jQuery available to your script , ie possibly the JavaScript / jQuery / AJAX haven't completed rendering the HTML DOM .根据讨论JavaScript/jQuery - “$ is not defined- $function()” 错误@Ketan 提到当您没有使 jQuery 可用于您的脚本时会发生此错误,即可能JavaScript / jQuery / AJAX尚未完成呈现HTML DOM


Solution解决方案

In these cases there are 3(three) different approaches available to solve the issue as follows:在这些情况下,有 3(三种)不同的方法可用于解决问题,如下所示:

  • The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag within the <head> section as follows: jQuery库是单个 JavaScript 文件,您可以使用<head>部分中的 HTML <script>标记引用它,如下所示:

     <head> <script src="jquery-3.4.1.min.js"></script> </head>

    This goes out and gets the jQuery code from the source.这会从源代码中获取jQuery代码。

Note : You do not have to include type="text/javascript" inside the <script> tag as this is not required in HTML5.注意:您不必在<script>标记中包含type="text/javascript" ,因为这在 HTML5 中不是必需的。 JavaScript is the default scripting language in HTML5 and in all modern browsers. JavaScript 是 HTML5 和所有现代浏览器中的默认脚本语言。

You can find a detailed discussion in Selenium: How selenium identifies elements visible or not?您可以在Selenium 中找到详细讨论: selenium 如何识别可见或不可见的元素? Is is possible that it is loaded in DOM but not rendered on UI? 是否有可能在 DOM 中加载但未在 UI 上呈现?


This usecase这个用例

As you mentioned, the following line fails:正如您所提到的,以下行失败:

Coords elementCoords = ofElement(driver, element);

This method takes one of the arguments (last) as element , but while defining you seem to be treating it as a list of elements, as in:此方法将其中一个参数 (last) 作为element ,但在定义时您似乎将其视为元素列表,如下所示:

@SuppressWarnings("UnusedDeclaration")
public Set<Coords> ofElements(WebDriver driver, WebElement... elements) {
    return ofElements(driver, Arrays.asList(elements));
}   

Seems some mismatch in argument types here.这里的参数类型似乎有些不匹配。


Additional considerations其他注意事项

You need to take care of a couple of things more:您还需要注意以下几点:

  • You are using chromedriver=78.0.3904.105您正在使用chromedriver=78.0.3904.105
  • Release Notes of chromedriver=78.0 clearly mentions the following : chromedriver=78.0 的发行说明清楚地提到了以下内容:

Supports Chrome version 78支持Chrome 78 版

Supports Chrome version 79支持Chrome 79 版

  • Your Selenium Client version is 3.14.0 of 2018-08-02T20:19:58.91Z which is almost 1.5 years older.您的Selenium Client版本是2018-08-02T20 :19:58.91Z 的 3.14.0它早了将近1.5年。
  • Your JDK version is 11.0.2 .您的JDK 版本11.0.2

So there is a clear mismatch between JDK v8u111 , Selenium Client v3.3.1 , ChromeDriver v2.41 and the Chrome Browser v79.0因此, JDK v8u111Selenium Client v3.3.1ChromeDriver v2.41Chrome Browser v79.0之间存在明显的不匹配


Solution解决方案

Ensure that:确保这件事:

I found a solution here, https://medium.com/virtualmind-io/jquery-injection-for-selenium-automation-tests-f6121ea57993 .我在这里找到了一个解决方案, https://medium.com/virtualmind-io/jquery-injection-for-selenium-automation-tests-f6121ea57993 Basically it injects jquery into the page.基本上它将jquery注入页面。 So I followed the steps and created this method that i used before calling AShot methods and could solve this issue, leaving the code here:所以我按照步骤创建了这个我在调用AShot方法之前使用的方法,可以解决这个问题,把代码留在这里:

        JavascriptExecutor js = (JavascriptExecutor) driver;
        if(!(Boolean) js.executeScript("return (typeof jQuery != \"undefined\")")) {
            js.executeScript(
                    "var headID = document.getElementsByTagName('head')[0];" +
                            "var newScript = document.createElement('script');" +
                            "newScript.type = 'text/javascript';" +
                            "newScript.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js';" +
                            "headID.appendChild(newScript);");
            WebDriverWait waitJQ = new WebDriverWait(driver, 30);
            Function<WebDriver, Boolean> jQueryAvailable = WebDriver -> (
                    (Boolean) js.executeScript("return (typeof jQuery != \"undefined\")")
            );
            waitJQ.until(jQueryAvailable);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.openqa.selenium.JavascriptException:SyntaxError:&#39;&#39;字符串文字包含通过Selenium使用executeScript时的转义换行符 - org.openqa.selenium.JavascriptException: SyntaxError: '' string literal contains an unescaped line break while using executeScript through Selenium Selenium、java、js“线程“主”org.openqa.selenium.JavascriptException 中的异常:在参数列表后丢失) - Selenium, java, js "Exception in thread "main" org.openqa.selenium.JavascriptException: missing ) after argument list" org.openqa.selenium.WebDriverException: disconnected: not connected to DevTools error using ChromeDriver Chrome using Selenium and Java - org.openqa.selenium.WebDriverException: disconnected: not connected to DevTools error using ChromeDriver Chrome using Selenium and Java org.openqa.selenium.WebDriverException:未知错误:无法使用ChromeDriver Selenium和Java聚焦元素 - org.openqa.selenium.WebDriverException: unknown error: cannot focus element using ChromeDriver Selenium and Java org.openqa.selenium.WebDriverException:未知错误:chrome 无法通过 Java 开始使用 Selenium ChromeDriver 和 Chrome - org.openqa.selenium.WebDriverException: unknown error: chrome failed to start using Selenium ChromeDriver and Chrome through Java org.openqa.selenium.NoSuchSessionException:在使用 ChromeDriver Chrome 和 Java 的 Selenium 自动化测试中没有这样的会话错误 - org.openqa.selenium.NoSuchSessionException: no such session error in Selenium automation tests using ChromeDriver Chrome with Java org.openqa.selenium.NoSuchSessionException:会话 ID 为空。 调用quit()后使用WebDriver? 通过 Java 使用 ChromeDriver Selenium 时出错 - org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? error using ChromeDriver Selenium through Java org.openqa.selenium.WebDriverException: disconnected: 在使用 Selenium 和 Chromedriver 的测试执行期间收到 Inspector.detached 事件错误 - org.openqa.selenium.WebDriverException: disconnected: received Inspector.detached event error during test execution using Selenium and Chromedriver org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04 - org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04 org.openqa.selenium.WebDriverException:无法使用Selenium和ChromeDriver访问Chrome - org.openqa.selenium.WebDriverException: chrome not reachable using Selenium and ChromeDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM