简体   繁体   中英

Calling Selenium from Jython PythonInterpreter is giving me error

I have installed Jython 2.7 beta version.

I have the code like this:

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!!");
    }
}

I'm trying to refer to the selenium package, which is been installed via like this:

pip install -U selenium

But when I run the above Java code, I was getting the following exception:

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

Then I found there is not selenium.py in the Lib directory of the Jython installation. So what I did is copied all the selenium code from pip installation location to Jython Lib . Like

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

After this I ran the following code, I get:

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

Now I'm struck. I dont know how to resolve this problem.

Any idea? Where I'm making mistake?

This may not solve your problem but suggest you change your path so that it is using the pip that comes installed with jython2.7. This should properly install selenium in the same directory as jython.

There might be other variables you can set to pick where you installed components using the other pip, but I don't know what those are.

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\\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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