【问题标题】:Calling Selenium from Jython PythonInterpreter is giving me error从 Jython PythonInterpreter 调用 Selenium 给了我错误
【发布时间】:2015-01-12 14:12:28
【问题描述】:

我已经安装了 Jython 2.7 测试版。

我有这样的代码:

import org.python.util.PythonInterpreter;
import org.python.core.*;

public class SimpleEmbedded {
    public static void main(String []args)
            throws PyException
    {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("from selenium import webdriver");
        System.out.println("Hello world!!");
    }
}

我正在尝试引用 selenium 包,它是通过以下方式安装的:

pip install -U selenium

但是当我运行上面的 Java 代码时,我得到了以下异常:

Exception in thread "main" Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named selenium

然后我发现Jython安装的Lib目录下没有selenium.py。所以我所做的就是将所有 selenium 代码从 pip 安装位置复制到 Jython Lib。喜欢

cp -r /Library/Python/2.7/site-packages/selenium/* ~/jython/Lib

在此之后我运行以下代码,我得到:

Exception in thread "MainThread" Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name webdriver

现在我很震惊。我不知道如何解决这个问题。

有什么想法吗?我哪里出错了?

【问题讨论】:

    标签: java python pip jython


    【解决方案1】:

    这可能无法解决您的问题,但建议您更改路径,使其使用 jython2.7 随附的 pip。这应该正确地将 selenium 安装在与 jython 相同的目录中。

    您可能可以设置其他变量来选择使用其他 pip 安装组件的位置,但我不知道这些是什么。

    c:\jython2.7.0\bin>echo %PATH% C:\jython2.7.0\bin;C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Oracle\Java\j avapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\Syste m32\WindowsPowerShell\v1.0\

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多