RobotFramework-当selenium org.openqa.selenium添加到我的JavaLibrary时,ClassNotFound异常

RobotFramework-当selenium org.openqa.selenium添加到我的JavaLibrary时,ClassNotFound异常,selenium,robotframework,selenium2library,Selenium,Robotframework,Selenium2library,请帮我解决这个问题。我在Java中遵循了以下步骤,并将这个jar文件添加到Robot框架项目中。执行测试时,我收到ClassNotFoundException Java更正代码: import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.openqa.selenium.WebElement; public class StoreElements{ public sta

请帮我解决这个问题。我在Java中遵循了以下步骤,并将这个jar文件添加到Robot框架项目中。执行测试时,我收到ClassNotFoundException

Java更正代码:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.WebElement;

public class StoreElements{
    public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
    public String storeTexts(List<WebElement> text){
    for(WebElement theElement : text){
     System.out.println("The Element class is: 
     "+theElement.getAttribute("class")+", text: " + theElement.getText());
 }
return ((WebElement) text).getText();
 }
} 
这是不是像我需要在Java项目中包含Selenium2Library而不仅仅是selenium,还是可能在Java中包含Selenium2Library

我收到的例外是

[ ERROR ] Unexpected error: NoClassDefFoundError: 
org/openqa/selenium/WebElement
java.lang.NoClassDefFoundError: 
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.getMethods(Class.java:1615)
at robot.utils.importer$py.import_$32(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py:274)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py)
at 
robot.utils.importer$py._import_class_or_module$5(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py:77)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py)
at robot.utils.importer$py.import_class_or_module$4(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py:74)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\importer.py)
at robot.model.visitor$py.visit_suite$2(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py:88)
at robot.model.visitor$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py)
at robot.model.testsuite$py.visit$19(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py:161)
at robot.model.testsuite$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py)
at robot.model.itemlist$py.visit$11(C:\jython2.7.0\Lib\site-
packages\robot\model\itemlist.py:75)
at robot.model.itemlist$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\itemlist.py)
at robot.model.visitor$py.visit_suite$2(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py:88)
at robot.model.visitor$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\visitor.py)
at robot.model.testsuite$py.visit$19(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py:161)
at robot.model.testsuite$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\model\testsuite.py)
at robot.utils.application$py._execute$10(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py:94)
at robot.utils.application$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py)
at robot.utils.application$py.execute_cli$5(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py:49)
at robot.utils.application$py.call_function(C:\jython2.7.0\Lib\site-
packages\robot\utils\application.py)
at runpy$py._run_code$9(C:\jython2.7.0\Lib\runpy.py:73)
at runpy$py.call_function(C:\jython2.7.0\Lib\runpy.py)
at runpy$py._run_module_as_main$14(C:\jython2.7.0\Lib\runpy.py:161)
at runpy$py.call_function(C:\jython2.7.0\Lib\runpy.py)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebElement
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 191 more
根据Kootstra的建议,添加了selenium-server-standalone-3.3.1 jar文件,当前的RED.xml如下所示,这次在关键字“Store Text”下没有显示红线。只有在执行此操作时才会引发异常。也不是更长的异常消息。

控制台命令:

Command: C:\jython2.7.0\bin\jython.exe -J-Dpython.path=C:\jython2.7.0\Lib\site-packages -J-cp .;C:\Program Files\Java\jdk1.8.0_121\lib;C:\Program Files\Java\jdk1.8.0_121\jre\lib;;.;C:\Java\Jars_KCC\GetElementText.jar;C:\jython2.7.0\selenium-server-standalone-3.3.1 -m robot.run -P C:\jython2.7.0\Lib\site-packages\Selenium2Library -i SmokeTest --listener C:\Users\CON_RT~1\AppData\Local\Temp\RobotTempDir3926709749247362236\TestRunnerAgent.py:63501:False -s KCC_Automation.TestSuites.TestResource -t KCC_Automation.TestSuites.TestResource.logon to kcc with valid credentials -t KCC_Automation.TestSuites.TestResource.navigate to customer's page -t KCC_Automation.TestSuites.TestResource.display customer names -t KCC_Automation.TestSuites.TestResource.stop testing C:\Java\Workspace\KCC_Automation
Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_121)
我创建了一个新项目,然后只在类路径中添加了“c:\jython2.7.0\selenium-server-standalone-3.3.1\”以及Selenium2库和自己的java库。我还尝试在jar文件的末尾添加.jar。它仍然有例外。但有一点是,现在它不再显示

这是控制台命令

Command: C:\jython2.7.0\bin\jython.exe -J-Dpython.path=C:\jython2.7.0\Lib\site-packages -J-cp .;C:\Java\JarsKCC\GetElementList.jar;C:\jython2.7.0\selenium-server-standalone-3.3.1.jar -m robot.run -P C:\jython2.7.0\Lib\site-packages\Selenium2Library -i SmokeTest --listener C:\Users\CON_RT~1\AppData\Local\Temp\RobotTempDir9154227905398549031\TestRunnerAgent.py:54485:False -s KCC_Automation.TestSuites.TestResource -t KCC_Automation.TestSuites.TestResource.logon to kcc with valid credentials -t KCC_Automation.TestSuites.TestResource.navigate to customer's page -t KCC_Automation.TestSuites.TestResource.display customer names -t KCC_Automation.TestSuites.TestResource.stop testing C:\Java\Workspace\KCC_Automation 
Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_121)

这次例外情况不同。ClassCastException:org.python.core.PyObjectDerived无法强制转换为org.openqa.selenium.WebElement

在我看来,这个错误非常简单:
[error]意外错误:NoClassDefFoundError:org/openqa/selenium/WebElement
它缺少这个类。这是一个在常规
selenium-server-standalone-x.x.x.jar
中找到的类

在过去的几天里,您在RED和Jython的上下文中提出了一些问题,我将假设这是您的开发环境。这就是说,如果恰好不同,步骤仍然适用,但应用方式不同

我已经将您的java类转换为Jar文件。在红色中,我创建了一个robot项目,并将其添加为库文件。在这篇文章中,我一步一步地详细介绍了这个过程

因为机器人脚本不完整,所以我对其进行了补充,使其现在看起来像这样:

*** Settings *** 
Library    StoreElements
Library    Selenium2Library

*** Test Cases ***
Get the customer Names
    @{customers}    Get Webelements    xpath=//div[contains(@class,'name-column')]
    Store Texts  ${customers}
需要添加库来确保关键字被识别,这意味着库被正确加载

因为新的Jar文件引用Selenium服务器类,所以需要能够访问它。这要求该类是类路径的一部分。在红色中,这可以通过将
selenium-server-standalone-x.x.x.jar
添加到RED.xml项目文件中的类路径来实现

这应该是这样的:

*** Settings *** 
Library    StoreElements
Library    Selenium2Library

*** Test Cases ***
Get the customer Names
    @{customers}    Get Webelements    xpath=//div[contains(@class,'name-column')]
    Store Texts  ${customers}

尽管此robot文件在运行时会失败,但如果您希望在使用Jython和robot Framework时从命令行指定java路径(红色):

C:\jython2.7.0\bin\jython.exe 
 -J-Dpython.path=C:\jython2.7.0\Lib\site-packages 
 -J-cp 
   .;
   C:\Eclipse\Workspace\JavaSelenium\ExampleLibrarySelenium.jar;
   C:\Eclipse\Workspace\ExampleJavaSelenium\selenium-server-standalone-3.3.1.jar 
 -m robot.run 
 -P C:\jython2.7.0\Lib\site-packages\Selenium2Library 
 --listener C:\Users\User\AppData\ ... \TestRunnerAgent.py:54540:False 
 -s JavaSelenium.Example 
 C:\Eclipse\Workspace\JavaSelenium
在上面格式化的命令中,类路径由
-J-cp
参数定义。添加的自定义Java Robot库
ExampleLibrarySelenium.jar
和额外需要的依赖项
selenium-server-standalone-3.3.1.jar
补充了标准
,该标准指的是
jython.exe
所在的文件夹


虽然将jar文件添加到现有的
文件夹也会起作用,但最好为每个项目添加所需的文件,以确保添加正确的版本,同时允许进行本地实验。

您的假设是正确的。我的开发环境是RED和Jython。我的结尾缺少的部分没有添加到jar文件selenium-server-standalone-3.3.1.jar中,而是现在添加的。但还是失败了。还使用我项目中的当前设置更新了问题。请检查并更正我的问题。您可以共享RED生成的命令吗?jar文件需要是其中定义的java类路径的一部分。正如我前面提到的,如果将jar文件放在与
jython.exe
相同的文件夹中,它也应该可以工作。这不是推荐的设置,但工作设置使更改为更好的设置比非工作设置更容易。附加了RED.xml的屏幕截图,执行时显示了错误。jython.exe位于路径C:/jython2.7.0/中,我已在其中更新selenium-server-standalone-3.3.1.jar。尽管屏幕截图没有为我提供用于启动Robot Framework的控制台命令,屏幕截图确实向我显示了添加到java类的路径,如
c:/jython2.7.0/selenium-server-standalone-3.3.1
,我认为这是一个目录。如果是这种情况,则应将其替换为
c:\jython2.7.0\selenium-server-standalone-3.3.1\
,并且您的设置应该可以运行。在问题中添加了console命令。此外,我还替换了上述路径,刷新并重新执行了相同的路径。但它还是不听我的。