简体   繁体   English

使用 java/selenium webdriver 操作类引发的 InvocationTargetException

[英]InvocationTargetException thrown with java/selenium webdriver action class

I'm using IE driver with java.我正在使用带有 java 的 IE 驱动程序。 I've done this fine in c# but trying java and hitting a snag.我在 c# 中做得很好,但尝试 java 并遇到了障碍。 I instantiate the driver with a singleton pattern at the class level and then begin a junit test with the @Test.我在类级别使用单例模式实例化驱动程序,然后使用@Test 开始一个junit 测试。 I separated the finding of the element out from the action and the finding of the element works great identifying the element with id=69 and the hexidecimal code the browser auto-assigns it.我将元素的发现与动作分开,元素的发现很好地识别了 id=69 的元素和浏览器自动分配的十六进制代码。 So far so good.到目前为止,一切都很好。

Then I take that variable for the WebElement and pass it into the action.click(element);然后我为 WebElement 获取该变量并将其传递给action.click(element); At this point an InvocationTargetException is thrown with a null.此时会抛出InvocationTargetException并返回 null。 Researching I have found where the driver null is causing an issue, but like I said driver is not null and has an element variable that populates accurately.研究我发现驱动程序 null 导致问题的位置,但就像我说的驱动程序不是 null 并且具有准确填充的元素变量。

I'm wondering if there is something wrong with the selenium reference or something behind the scenes that may be throwing null when trying to utilize the library.我想知道硒参考是否有问题,或者在尝试使用该库时可能会抛出 null 的幕后问题。 Any ideas where I might look to get this to work?我有什么想法可以让它发挥作用吗? This is technically a java.lang.reflect exception and not a selenium specific exception, which if it was a null variable I would expect it to throw a selenium exception.这在技术上是一个 java.lang.reflect 异常,而不是一个特定于 selenium 的异常,如果它是一个空变量,我希望它会抛出一个 selenium 异常。

Any pointers or thoughts would be appreciated.任何指示或想法将不胜感激。

I found the answer.我找到了答案。 The actions class was unbound to the driver.动作类未绑定到驱动程序。 So I could use the driver all I wanted, but the actions initiation couldn't find the browser to execute the actions on.所以我可以使用我想要的所有驱动程序,但是操作启动找不到浏览器来执行操作。 I had a sub-class named Action that I was working with which confused me with the "Actions" class that was selenium.我有一个名为 Action 的子类,我正在使用它,这让我与 selenium 的“Actions”类混淆了。

Oops, but if anyone runs into that InvocationTargetException with a null value while trying to utilize selenium, be sure that each selenium class is properly instantiated and associated with the active driver instance.糟糕,但如果有人在尝试使用 selenium 时遇到带有 null 值的InvocationTargetException ,请确保每个 selenium 类都已正确实例化并与活动驱动程序实例相关联。

我使用了下面对我有用的代码。

((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM