Basic Python Script to Log in to Website Using Selenium Webdriver

Using Selenium for automated website testing. 

I was working with Selenium (well, Python) scripts for several years now, this article aims to provide two basic Python scripts that can be used to log in and then log out from a website. The first script is suitable for a system that has an X server running, while the second one utilises a Virtual Framebuffer “fake” X server.

Software

Software used in this article:

  1. Debian 9
  2. Python 2.7.13
  3. Selenium 3.8.0
  4. Firefox ESR 52.5

Scripts should work on any modern Debian/Ubuntu system.

Installation

Install Selenium and Firefox:

# apt-get install python-pip
# pip install -U selenium
# apt-get install firefox-esr

Or go for Iceweasel instead:

# apt-get install iceweasel

We will need the latest geckodriver:

# wget https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.18.0-linux64.tar.gz
# tar xf geckodriver-v0.18.0-linux64.tar.gz -C /usr/local/bin/
# chown root:root /usr/local/bin/geckodriver
# chmod 755 /usr/local/bin/geckodriver

Python Scripts

For Systems with an X Server

The content of the python script selenium.py can be seen below. The script will open Firefox, therefore you need to have an X server running.

#!/usr/bin/env python
#
# Author: Tomas (www.lisenet.com)
#
# CHANGELOG
#
# [v0.1] - 2013
# Initial release, script uses one hardcoded URL

from selenium import webdriver
import time, subprocess

def main():

    # You most certainly want to change this
    url = "https://example.com"

    profile = webdriver.FirefoxProfile()
    # Set a user agent string to help parse webserver logs easily
    profile.set_preference("general.useragent.override", "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 selenium.py")
    browser = webdriver.Firefox(profile)
    browser.get(url)
    time.sleep(1)

    # The element names will likely be different for your application,
    # therefore change accordingly
    user = browser.find_element_by_name("j_username")
    password = browser.find_element_by_name("j_password")
    # Clear the input fields
    user.clear()
    password.clear()
    user.send_keys("username")
    password.send_keys("password")
    time.sleep(1)
    browser.find_element_by_id("submit").click()

    # Keep the page loaded for 8 seconds
    time.sleep(8)

    # Log out
    # The element IDs will likely be different for your application,
    # therefore change accordingly
    browser.find_element_by_id("logout_link").click()
    time.sleep(2)
    browser.find_element_by_id("dialog_button_ok").click()
    time.sleep(1)

    browser.delete_all_cookies()
    browser.close()
            
if __name__ == '__main__':
    main()

To run the script, use the following:

$ python ./selenium.py

For Systems without an X Server

You most likely want to use a server to perform automated testing, therefore you need a fake X server:

# apt-get install xvfb

The content of the python script selenium_xvfb.py that uses the fake X server can be seen below.

#!/usr/bin/env python
#
# Author: Tomas (www.lisenet.com)
#
# CHANGELOG
#
# [v0.1] - 2013
# Initial release, script uses one hardcoded URL

from selenium import webdriver
import time, subprocess, os, signal

def main():

    killXvfb()
    os.system('/usr/bin/Xvfb :11 -ac -screen 0 1024x768x24 &')
    os.environ['DISPLAY'] = ':11.0'

    # You most certainly want to change this
    url = "https://example.com"

    profile = webdriver.FirefoxProfile()
    # Set a user agent string to help parse webserver logs easily
    profile.set_preference("general.useragent.override", "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 selenium_xvfb.py")
    browser = webdriver.Firefox(profile)
    browser.get(url)
    time.sleep(1)

    # The element names will likely be different for your application, 
    # therefore change accordingly
    user = browser.find_element_by_name("j_username")
    password = browser.find_element_by_name("j_password")
    # Clear the input fields
    user.clear()
    password.clear()
    user.send_keys("username")
    password.send_keys("password")
    time.sleep(1)
    browser.find_element_by_id("submit").click()

    # Keep the page loaded for 8 seconds
    time.sleep(8)

    # Log out
    # The element IDs will likely be different for your application, 
    # therefore change accordingly
    browser.find_element_by_id("logout_link").click()
    time.sleep(2)
    browser.find_element_by_id("dialog_button_ok").click()
    time.sleep(1)

    browser.delete_all_cookies()
    browser.close()
    killXvfb()

def killXvfb():
    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out, err = p.communicate()

    for line in out.splitlines():
        if 'Xvfb' in line:
            pid = int(line.split(None, 1)[0])
            os.kill(pid, signal.SIGKILL)


if __name__ == '__main__':
    main()

To run the script, use the following:

$ python ./selenium_xvfb.py

4 thoughts on “Basic Python Script to Log in to Website Using Selenium Webdriver

  1. Hi,

    I try the 2 solutions

    with an X Server, i got this error:
    … selenium.common.exceptions.WebDriverException: Message: connection refused

    without an Xserver even if firefox started, i got this error:
    .. selenium.common.exceptions.WebDriverException: Message: connection refused

    please help
    regards

  2. Hi Tomas i need selenium configuration in centos 7
    selenium python webdriver in rhel 7
    please help me

    how to run python script in selenium using centos 7
    what are the requirments and what is configuration please help me

Leave a Reply

Your email address will not be published. Required fields are marked *