简体   繁体   English

java.lang.ClassCastException: com.sun.proxy.$Proxy8 不能转换为 org.openqa.selenium.internal.WrapsDriver

[英]java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to org.openqa.selenium.internal.WrapsDriver

I have following pointcut and the given advice in AspectJ我在 AspectJ 中有以下切入点和给定的建议

@Pointcut("(call(* org.openqa.selenium.WebElement.sendKeys(..)))")
    public void onWebElementAction() {
}

@After("onWebElementAction() && target(webelement)")
public void afterWebElementAction(JoinPoint joinPoint, WebElement webelement) {
    System.out.println(webelement.getAttribute("name")); //1
    WebDriver driver = ((WrapsDriver) webelement).getWrappedDriver(); //2
    //DO SOMETHING HERE
}

While the line 1 is executed without any error.虽然第 1 行执行时没有任何错误。 It is on line 2 I get error在第 2 行我收到错误

java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to org.openqa.selenium.internal.WrapsDriver

The casting works in other places without issues.铸造在其他地方工作没有问题。 Can someone please help?有人可以帮忙吗?

While the answer flagged as correct does point out the issue, it doesn't explain the issue nor suggests a solution which does actually exist.虽然标记为正确的答案确实指出了问题,但它并没有解释问题,也没有提出实际存在的解决方案。 Let me begin with giving a bit more of detail around the underlying issue here which is the way the WebElement could have been instantiated.首先让我详细介绍一下这里的潜在问题,即WebElement可以实例化的方式。

On one hand, when a WebElement gets instantiated as the result of a call to WebDriver#findElement , the actual RemoteWebElement object gets constructed at that very moment, however, when a WebElement gets instantiated via PageFactory#initElements , the actual concrete class object ( RemoteWebElement ) doesn't get created at that point but instead a proxy does.一方面,当WebElement作为调用WebDriver#findElement的结果被实例化时,实际的RemoteWebElement对象会在那一刻被构造,但是,当WebElement通过PageFactory#initElements被实例化时,实际的具体类对象( RemoteWebElement ) 不会在那时创建,而是代理创建。

Here is where the main issue relies.这是主要问题所依赖的地方。 The proxy object does NOT implement the WrapsDriver interface and that is why the cast exception is thrown, which is perfectly fine.代理对象不实现WrapsDriver接口,这就是抛出WrapsDriver异常的原因,这很好。 However, if you are curious enough to see how the actual proxy creation is done (at least by the default decorator), you will see that the object instantiated as the proxy does instead implement the WrapsElement interface which does offer the method getWrappedElement so, with it, you can extract the underlying WebElement and then with this, extract the underlying WebDriver , just as you are trying.但是,如果您对实际代理创建是如何完成的(至少通过默认装饰器)感到好奇,您将看到作为代理实例化的对象确实实现了WrapsElement接口,该接口确实提供了getWrappedElement方法,因此,它,您可以提取底层WebElement ,然后使用此方法提取底层WebDriver ,就像您正在尝试一样。

Now, the key here is that any WebElement instantiated via WebDriver#findElement does not implement WrapsElement because it is the actual element and not a proxy so, before you attempt using WrapsElement#getWrappedElement , you first need to check if the passed WebElement is actually a proxy or not.现在,这里的关键是任何通过WebDriver#findElement实例化的WebElement都不会实现WrapsElement因为它是实际元素而不是代理,因此,在尝试使用WrapsElement#getWrappedElement ,首先需要检查传递的WebElement是否实际上是一个代理与否。

You can achieve this with reflection, ie你可以通过反射来实现这一点,即

if(WrapsElement.class.isAssignableFrom(element.getClass()))
  webDriver = ((WrapsDriver)((WrapsElement)element).getWrappedElement()).getWrappedDriver();
else
  webDriver = ((WrapsDriver)element).getWrappedDriver();

tl;dr The WebElement instance you are using was instantiated via PageFactory#initElements and you first need to extract the underlying WebElement with WrapsElement#getWrappedElement and then the WebDriver from it. tl;dr 您正在使用的WebElement实例是通过PageFactory#initElements实例化的,您首先需要使用WrapsElement#getWrappedElement提取底层WebElement ,然后从中提取WebDriver

This is a wild guess since I don't see a case where it actually worked.这是一个疯狂的猜测,因为我没有看到它实际起作用的案例。 From the exception it seems that the WebElement that is being passed to afterWebElementAction is initialized via PageFactory .从异常似乎正在传递到WebElement afterWebElementAction通过初始化PageFactory My guess is that if you pass WebElement derived from driver.findElement(), to afterWebElementAction , you wouldn't get casting exception.我的猜测是,如果您将从 driver.findElement() 派生的 WebElement 传递给afterWebElementAction ,您将不会遇到强制转换异常。 This is how it must be working for you in other cases most likely.这就是它在其他情况下最有可能为您工作的方式。

暂无
暂无

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

相关问题 原因:java.lang.ClassCastException:com.sun.proxy.$Proxy67 无法转换为 org.omg.CORBA.Z497031794414A552435F90151BZAC3 - Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy67 cannot be cast to org.omg.CORBA.Object java.lang.ClassCastException:com.sun.proxy。$ Proxy219无法转换为org.springframework.data.redis.connection.StringRedisConnection - java.lang.ClassCastException: com.sun.proxy.$Proxy219 cannot be cast to org.springframework.data.redis.connection.StringRedisConnection java.lang.ClassCastException: com.sun.proxy.$Proxy0 不能转换为 org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate - java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate java.lang.ClassCastException:com.sun.proxy。$ Proxy1无法转换为 - java.lang.ClassCastException: com.sun.proxy.$Proxy1 cannot be cast to java.lang.ClassCastException:com.sun.proxy。$ Proxy47无法转换为 - java.lang.ClassCastException: com.sun.proxy.$Proxy47 cannot be cast to RMI:java.lang.ClassCastException:com.sun.proxy。$ Proxy1无法转换为Funktion - RMI: java.lang.ClassCastException: com.sun.proxy.$Proxy1 cannot be cast to Funktion java.lang.ClassCastException:com.sun.proxy。$ Proxy29无法转换为com.frodo.questionbank.service.impl.QuestionService - java.lang.ClassCastException: com.sun.proxy.$Proxy29 cannot be cast to com.frodo.questionbank.service.impl.QuestionService 线程“main”中的异常 java.lang.ClassCastException:类 com.sun.proxy.$Proxy0 不能转换为类船员 - Exception in thread “main” java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class crewcut java:java.lang.ClassCastException:com.sun.proxy.$Proxy0 类无法转换为 com.sun.tools.javac.processing.JavacProcessingEnvironment 类 - java: java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class com.sun.tools.javac.processing.JavacProcessingEnvironment Spring XML 5.1.0 java.lang.ClassCastException:com.sun.proxy。$ Proxy9 - Spring XML 5.1.0 java.lang.ClassCastException: com.sun.proxy.$Proxy9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM