Selenium-Python Client Library - Automation in the Background

I am trying to automate the login process to my web application using the Selenium-Python client library. The ultimate goal is to learn the Selenium Python client library. Therefore, I would be very grateful for the answers from those who fall into Selenium-Python.

I current have this code:

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 everything happens in the interface. I mean, it literally opens Firefox, keys in values, clicks on Submit, etc., as expected.

I'm just wondering if there is anything I can do to make this all happen in the background? Let's say I don't want to track what the script does. I just want it to run in the background. Is there any way to achieve this?

EDIT

We downloaded PyVirtualDisplay and installed it in my windows using the setup.py install command. Also installed EasyProcess module and Path.

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 following errors while 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! ` 
+6
source share
2 answers

Firefox (and other graphical browsers) requires an X-display. You can use virtual with PyVirtualDisplay :

 from pyvirtualdisplay import Display display = Display(visible=0, size=(1024, 768)) display.start() browser = webdriver.Firefox() ... more selenium code ... display.stop() 

In addition to PyVirtualDisplay, you will also need its xfvb and Xephyr dependencies (on debian: apt-get install -y xvfb xserver-xephyr )

+8
source

You can also use PhantomJS and Ghostdriver to run Selenium without a graphical browser.

https://github.com/detro/ghostdriver

+1
source

Source: https://habr.com/ru/post/894177/


All Articles