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_install开发者_如何学编程ed
raise EasyProcessCheckInstalledError(self)
EasyProcessCheckInstalledError: cmd=['Xephyr', '-help']
OSError=[Error 2] The system cannot find the file specified
Program install error! `
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
)
You can also use PhantomJS and Ghostdriver to run Selenium without a GUI-based browser.
https://github.com/detro/ghostdriver
精彩评论