Import Selenium into Sikuli script --- update for version SikuliX 1.1.1 as of December 2016

Asked by Eugene S

see https://answers.launchpad.net/sikuli/+question/404578 comment #1

---------------------------------------------------------------------

Hi,

I know that there are quite a few threads already concerned with using Sikuli and Selenium.
The thing is that it seems that most (if not all) people use Sikuli using Eclipse and/or import Sikuli into their Java code rather than the opposite way.

So since I am using pure Sikuli script from command line and Java is not my comfort area, I'd like to ask if that's still possible to somehow import Selenium functionality directly into Sikuli script?

I see that there are couple different Selenium packages available. For example, there is a pure java Selenium that includes jar files and there is a Python package as well (https://pypi.python.org/pypi/selenium).

So to sum up, what I was thinking of a way to use it just as it could be used from Python:

from selenium import webdriver

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
...

Is that even possible?
If yes, is that relatively simple?

If any specific thread already answers this question I will be happy to get a link, maybe I missed it.

Thanks in advance!
Eugene

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Have you already tried with the Python package, to use it with the latest Jython 2.7 (contained in SikuliX 1.0.1)?

Revision history for this message
RaiMan (raimund-hocke) said :
#2

Sikuli 1.0.1 not possible, because it contains Jython 2.5.4

I tested with version 1.1.0-Beta1 (not available yet :-(, which contains Jython 2.7:
at least
from selenium import webdriver

runs without errors.

but
browser = webdriver.Firefox()

complains:
[error] script [ testselenium ] stopped with error in line 49
[error] RuntimeError ( Could not find firefox in your system PATH. Please specify the firefox binary location or install firefox )
[error] --- Traceback --- error source first
line: module ( function ) statement
153: firefox_binary ( _get_firefox_start_cmd ) raise RuntimeError("Could not find firefox in your system PATH." +
43: firefox_binary ( __init__ ) self._start_cmd = self._get_firefox_start_cmd()

and
browser = webdriver.Safari()

complains:
[error] script [ testselenium ] stopped with error in line 52
[error] Exception ( No executable path given, please add one to Environment Variable 'SELENIUM_SERVER_JAR' )

and
browser = webdriver.Chrome()

complains:
[error] script [ testselenium ] stopped with error in line 59
[error] selenium.common.exceptions.WebDriverException ( Message: 'ChromeDriver executable needs to be available in the path. Please download from http://chromedriver.storage.googleapis.com/index.html and read up at http://code.google.com/p/selenium/wiki/ChromeDriver' )
[error] --- Traceback --- error source first
line: module ( function ) statement
65: service ( start ) raise WebDriverException(

I am testing this on Mac 10.9

So you might try with a standalone Jython 2.7 installation and try to solve these issues.

Revision history for this message
Eugene S (shragovich) said :
#3

Hi RaiMan,

Thank you for your answer.

I have just tried to do that:
  1. Installed Jython (according to the official instructions)
  2. Installed Python bindings for Selenium (selenium 2.41.0) from https://pypi.python.org/pypi/selenium

Now if I start a command line Python, I can import selenium successfully:

from selenium import webdriver
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page

However, when I start command line jython (by running "jython.bat") and try to do the same, I get and error:

>>> from selenium import webdriver
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named selenium

I'm not sure I've done everything right and I realize that this is not a Sikuli issue so if you see any immediate issue with how I'm doing that I will be happy to know. If not, I will look for answer elsewhere.

Thanks!
Eugene

Revision history for this message
RaiMan (raimund-hocke) said :
#4

ok, to use it with Sikuli you have to either
- put the path to the folder in the Python installation containing the selenium folder (the one containing the __init__.py) to sys.path

or
- copy this folder to a folder Lib in the folder where you have the SikuliX stuff (this is found automatically)

Revision history for this message
Eugene S (shragovich) said :
#5

Hi RaiMan,

After some time I have attempted to try to import Selenium again and done what you have recommended above:

1. Copy Selenium folder (with __init__.py) to "C:\SikuliX\Lib"
2. Run the script with the following import:

    from selenium import webdriver

At this point I had this line (within selenium.py) failing for some reason:
from __future__ import unicode_literals

But I found your answer where you say that this line is not needed since Jython does that already internally so I commented that out.

However, currently the problem that once I import the webdriver module as described above, I get the following exception:

C:\SikuliX\sikuli-script.jar -r Tests\Test.sikuli
[error] script stopped with error in line 58
[error] SyntaxError ( ("'with' will become a reserved keyword in Python 2.6",
('C:\\SikuliX\\Lib\\selenium\\webdriver\\remote\\webdriver.py', 691, 12, '')) )
Exception in thread "MainThread" java.lang.IllegalStateException: No match found

Clearly that means some versions mismatch, But is there any way to fix such issue? Will be glad to hear any ideas!

Thanks in advance!
Eugene S

Revision history for this message
Eugene S (shragovich) said :
#6

Just found this question with similar issue:
https://answers.launchpad.net/sikuli/+question/192180

However still not sure how to (if at all possible) to fix it.

Revision history for this message
Eugene S (shragovich) said :
#7

Some more investigation I have done in the last couple of hours and found couple of issues that occur in Selenium because of versions mismatch:

1. In some selenium files (specifically in Selenium\webdriver\remote\webdriver.py) a "with" is used when opening files. That's not supported by Jython and I had to rewrite that part in Selenium to avoid the exception. So instead:

with open(filename, 'wb') as f:
                f.write(png)

it became:
file = open(filename, 'wb')
file.write(png)

2. "as" is not supported by Jython as well. So in certain cases (like in Selenium\webdriver\remote\utils.py), where exceptions are used like:

except IOError as err:
        LOGGER.error("Error in extracting webdriver.xpi: %s" % err)
        return None

I changed that to:
except IOError, err:
        LOGGER.error("Error in extracting webdriver.xpi: %s" % err)
        return None

3. I have already mentioned that the following import line fails as well and must be deleted from Selenium code:
from __future__ import unicode_literals

As a result I was able to eliminate certain errors and exceptions however I got stuck at a point when Selenium was trying to import json module which doesn't look available in Jython. Selenium using json quite extensively and bypassing it doesn't seem possible and I have no idea how to progress from here on (of course given the fact that I've done all the rest correctly).

Revision history for this message
RaiMan (raimund-hocke) said :
#8

I do not think, that it is a good idea, to adapt the selenium python sources.

What version of Jython are you running? The latest 2.7 should not have these problems.

With SikuliX version 1.0.1 we still have 2.5.4 bundled.
Version 1.1.0 has 2.7.

So I guess the best choice would be to run your stuff with 1.0.1 using sikuli-java.jar and an external Jython 2.7.

You might as well try 1.1.0-Beta4 later today.

Revision history for this message
Eugene S (shragovich) said :
#9

Thanks for the answer RaiMan.
I agree with you completely that playing with Selenium is a bad idea. It was my last resort (just really wanted it to work :) )

The Jython I am running is the one bundled with Sikuli 1.0.1 and currently I have it configured and running smoothly on a couple machines so I'd prefer to stay with this version for a while just to avoid new issues with new (1.1.0) version.

BTW, how smooth should be a process of upgrading 1.0.1 to 1.1.0 on such machines, where everything already configured and working? These machines do not have internet access as well. (or I probably should have asked this as a separate question since maybe someone is interested in the same thing).

Regarding your last proposal (running stuff with 1.0.1 using sikuli-java.jar and an external Jython 2.7) sounds very promising but I have a very slight idea how to perform something like that. Could you please give me some starting point for doing this?

Thanks!
Eugene

Revision history for this message
RaiMan (raimund-hocke) said :
#10

-- 1.1.0
since it is still Beta, there is always a risk when upgrading.
I do not recommend to do that, but instead make some tests in "sandbox", so your current installation is not affected.

--- Jython 2.7
-- download from:
http://search.maven.org/remotecontent?filepath=org/python/jython-standalone/2.7-b2/jython-standalone-2.7-b2.jar

-- put it in a separate folder

--- go to that folder and run:
java -jar jython-standalone-2.7-b2.jar

… as a test: should open an interactive Jython session

--- to run with Sikuli features:
java -cp <path-to-sikuli-jar> -jar jython-standalone-2.7-b2.jar

<path-to-sikuli-jar> absolute path to sikuli-java.jar (1.0.1) or sikulixapi.jar (1.1.0)

again an interactive session should open.

enter:
import org.sikuli.basics.SikuliXforJython
from sikuli import *

the first sets up the Jython path for the usage of the Sikuli features, whereas the second is the known initialization.

running
java -jar jython-standalone-2.7-b2.jar -h

reveals some command line options, that might help (e.g. -Dsikuli.Debug=3)

Revision history for this message
Eugene S (shragovich) said :
#11

Thanks RaiMan!

I just started implementing your solution and I have 2 following issues/questions, both at the following step:

-- java -cp <path-to-sikuli-jar> -jar jython-standalone-2.7-b2.jar
    <path-to-sikuli-jar> absolute path to sikuli-java.jar (1.0.1) or sikulixapi.jar (1.1.0)

1. So first, the only Sikuli *.jar file I have in my "C:\SikuliX\" folder is: "sikuli-script.jar" and not like you have mentioned -> sikuli-java.jar

2. When I tried to run the following command:
java -cp "C:\SikuliX" -jar jython-standalone-2.7-b2.jar

I get the following error:

File "<stdin>", line 1
    java -cp "C:\SikuliX" -jar jython-standalone-2.7-b2.jar
            ^
SyntaxError: no viable alternative at input '"C:\SikuliX"'

Can you please advise?

Thanks!
Eugene

Revision history for this message
RaiMan (raimund-hocke) said :
#12

if you want to use 1.0.1 and do not have sikuli-java.jar, then you have to run setup with option 4 to get it.

sikuli-script.jar cannot be used, because it contains the Jython 2.5 version and might lead to crashes.

the -cp option always needs .jar file entries (separated by ;), folders are not accepted.

Revision history for this message
AJITH (aji4032) said :
#13

Hi RaiMan/Eugene,

2 years have passed since this thread was opened and I seemed to have been hit with the same issue :( . I would like to integrate selenium into my Sikuli script. Tried searching around and had lil or no luck.

I'm using SikuliX 1.1.0 and was wondering if anyone was able to find a workaround or find a solution to this issue. If so, can you please help me.

Thanks,
Ajith

Revision history for this message
RaiMan (raimund-hocke) said :
#14

@AJITH
please look at
https://answers.launchpad.net/sikuli/+question/404578 comment #1

and report experiences, suggestions and comments there please.

Can you help with this problem?

Provide an answer of your own, or ask Eugene S for more information if necessary.

To post a message you must log in.