selenium

Selenium is a web automation framework. It starts a web browser and any task that can be done typically on the web, Selenium+Python can do for you.

selenium

Related course

Articles

Note
To use Selenium you need either Python version:

Python 2.6, 2.7
Python 3.3+

To check your version use:

python --version

Installation of a web driver is mandatory, without installing one the browser wont start even though one is installed on your computer.

Selenium install

Selenium is a web automation tool. A web browser can be controlled using Python code, any task you would normally do on the web can be done using the selenium module.

Related course
Selenium WebDriver and Python: WebTest Automation Course

Installation
To install the selenium module, type the command:

pip install -U selenium

Then get the web driver from http://docs.seleniumhq.org/projects/webdriver. There are all kind of webdrivers including:

  • ChromeDriver
  • FirefoxDriver
  • RemoteWebDriver

After installation of the web driver, we can make Python start the browser using:

from selenium import webdriver
import time
 
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://python.org')

This will start the Chromium browser and open the python site.

selenium chromium
Selenium will start the chromium browser automatically

selenium webdriver

Selenium is a web automation framework that can be used to automate website testing. Because Selenium starts a webbrowser, it can do any task you would normally do on the web.

Related course
Selenium WebDriver and Python: WebTest Automation Course

Web Driver
To start a web browser, the Selenium module needs a web driver.
Supported browsers are:

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari
  • Opera
  • PhantomJS (invisible)

To start a browser, you will need to corresponding driver. The driver “ChromeDriver” is needed to start Chrome, “FirefoxDriver” for Firefox.

All drivers can be downloaded from: http://docs.seleniumhq.org/download/

Example code
Python will start and control the chromium browser using the code below:

from selenium import webdriver
import time
 
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://python.org')

You can change the browser using:

# Firefox 
driver = webdriver.Firefox()
 
# Google Chrome 
driver = webdriver.Chrome()
 
# iPhone
driver = webdriver.Remote(browser_name="iphone", command_executor='http://172.24.101.36:3001/hub')
 
# Android
driver = webdriver.Remote(browser_name="android", command_executor='http://127.0.0.1:8080/hub')

Selenium get source

Selenium is a web automation module that can be used to get a webpages html code. In this article we will show how to achieve that.

You may need to set the path to chromium:

export PATH=$PATH:/usr/lib/chromium/

Related course
Selenium WebDriver and Python: WebTest Automation Course

Get html source
We import webdriver from the selenium module. A webdriver object is created (chromium) and we can optionally specify if we want to ignore certificate errors. Once the web browser started we navigate it to a webpage URL using the get() module. Then we get the page source.

from selenium import webdriver
import time
 
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://python.org')
 
html = driver.page_source
print(html)

It will output the webpage source, which is stored in the variable html.

selenium chromium
Selenium will start the chromium browser automatically

selenium textbox

Given a webpage containing a text area or text field, text can be automatically written using Python code. In this article we will demonstrate this automation using a small code snippet.

Related course
Selenium WebDriver and Python: WebTest Automation Course

Selenium add textbox text
We start by importing the selenium module. The driver needs to be created starting the web browser. We open the target website using the method

driver.get(url)

We select the html element using the method

driver.find_element_by_id(id)

Then we write text in the box using the method:

send_keys(str)

Complete code:

from selenium import webdriver
import time
 
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://codepad.org')
 
text_area = driver.find_element_by_id('textarea')
text_area.send_keys("This text is send using Python code.")

The browser will start automatically adding the text:

selenium textbox
send_keys method writes in textbox

Selenium get links

Related course:

Selenium
Selenium automates browsers. The selenium module can make the browser do anything you want including automated testing, automating web tasks and data extraction. In this article we’ll use it for data mining, extracting the links from a web page.

Install it using:

pip install selenium

To use the module, you need a selenium web driver. All the popular browsers are supported including Chrome and Firefox.

After installing the web driver, you may need to add it to path:

export PATH=$PATH:/usr/lib/chromium/

Starting Selenium
Test if selenium is installed correctly using:

from selenium import webdriver
import time
 
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
 
driver.get('https://www.w3.org/')
time.sleep(3)
driver.quit()

Depending on your setup, you can start it without parameters:

webdriver.Chrome()

Extract links
To get links from webpage, use the code below:

from selenium import webdriver
 
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
 
driver.get('https://www.w3.org/')
for a in driver.find_elements_by_xpath('.//a'):
    print(a.get_attribute('href'))

Posts navigation

1 2