6

I am trying automate a login process to my web application using Selenium-Python Client Library. The ultimate goal is to learn Selenium's Python Client Library. So, I would really appreciate answers from those who are into Selenium-Python.

I current have a code like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://myServer/WebAccess/login.html") # Load Application page
elem = browser.find_element_by_name("LoginID") # Find the Login box
elem.send_keys("Administrator")
elem = browser.find_element_by_name("Password") # Find the Password box
elem.send_keys("Administrator" + Keys.RETURN)

This works well but all happens in the front-end. I mean it literally opens up a Firefox, keys in the values, clicks on Submit, etc which is as expected.

I am just wondering, is there anything I can do to make all this happen at the background? Let us say I do not want to monitor what the script is doing. I just want it to run in the background. Is there any way to achieve this?

EDIT

Downloaded PyVirtualDisplay and installed it in my windows using the command setup.py install. Installed EasyProcess and Path module too.

Now I have a sample code like this

from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(1024, 768))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.close()
display.stop()

I get the below errors on executing this code:

`Traceback (most recent call last):
  File "C:\Documents and Settings\user\Desktop\Sel.py", line 1, in <module>
    from pyvirtualdisplay import Display
  File "C:\Python27\lib\site-packages\pyvirtualdisplay\__init__.py", line 1, in <module>
    from display import Display
  File "C:\Python27\lib\site-packages\pyvirtualdisplay\display.py", line 2, in <module>
    from pyvirtualdisplay.xephyr import XephyrDisplay
  File "C:\Python27\lib\site-packages\pyvirtualdisplay\xephyr.py", line 8, in <module>
    EasyProcess([PROGRAM, '-help'], url=URL, ubuntu_package=PACKAGE).check_installed()
  File "C:\Python27\lib\site-packages\easyprocess\__init__.py", line 202, in check_installed
    raise EasyProcessCheckInstalledError(self)
EasyProcessCheckInstalledError: cmd=['Xephyr', '-help']
OSError=[Error 2] The system cannot find the file specified  
Program install error! `
2
  • 1
    PyVirtualDisplay requires Xephyr and Xvfb, which appear to be Linux-only services. I searched for a virtual display for Windows, but couldn't find anything suitable. Commented Aug 3, 2011 at 13:02
  • 1
    Jason, Thanks! I was searching too. I finally ended up with HtmlUnit to proceed with a GUI less approach. Thank you! Commented Aug 3, 2011 at 13:06

2 Answers 2

8

Firefox (and other graphical browsers) require an X display. You can use a virtual one with the help of PyVirtualDisplay:

from pyvirtualdisplay import Display
display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
... more selenium code ...

display.stop()

Apart from PyVirtualDisplay, you'll also need its dependencies xfvb and Xephyr (on debian: apt-get install -y xvfb xserver-xephyr)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion. I am on Windows XP. I installed the PyVirtualDisplay module and use the code as updated in my initial post under EDIT. I however get those errors as mentioned in the original post. Any suggestion?
I installed the EasyProcess module and the old error has gone now. It now says ` File "C:\Python27\lib\site-packages\pyvirtualdisplay\abstractdisplay.py", line 2, in <module> from path import path ImportError: No module named path` Where do I download Path module for Python from? Google-ing did not help me. or I did not Google it effectively
I even now managed to get the Path module from "pypi.python.org/pypi/path.py/2.2" but then when I executed the code (that I EDITed in my original post), I still get errors as edited in original post
1

You can also use PhantomJS and Ghostdriver to run Selenium without a GUI-based browser.

https://github.com/detro/ghostdriver

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.