python logo

selenium python


Python hosting: Host, run, and code Python in the cloud!

Selenium is a powerful web automation framework that allows users to automate website testing processes. With Selenium, initiating a web browser and performing tasks on the web becomes seamless and efficient.

If you’re beginning your journey with Selenium and browser automation, the course mentioned below is a highly recommended starting point.

Essential Course: Browser Automation with Python Selenium

Understanding the Web Driver in Selenium

For Selenium to initiate a web browser, it relies on a component called the web driver. Essentially, Python communicates with the Selenium web driver, which in turn interacts directly with the browser.

The browsers supported by Selenium include:

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari
  • Opera
  • PhantomJS (Runs invisibly)

In order to launch a specific browser, you’ll need its respective web driver. For instance, to initiate Chrome, you would require “ChromeDriver”, while for Firefox, you’d need “FirefoxDriver”.

You can acquire all necessary drivers here: Selenium Web Driver Downloads

Sample Code to Get Started:
The following Python code demonstrates how to launch and control the chromium browser using Selenium:

1
2
3
4
5
6
7
8
9
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 easily switch between browsers by creating a different instance. Here’s how:

1
2
3
4
5
6
7
8
9
10
11
# For Firefox:
driver = webdriver.Firefox()

# For Google Chrome:
driver = webdriver.Chrome()

# For iPhone:
driver = webdriver.Remote(browser_name="iphone", command_executor='http://172.24.101.36:3001/hub')

# For Android:
driver = webdriver.Remote(browser_name="android", command_executor='http://127.0.0.1:8080/hub')

The initial two lines of the above code will initiate the browser locally on your computer, while the subsequent lines demonstrate how to launch the browser remotely, such as on a smartphone.

You can navigate to a specific webpage using the get() method as shown below:

1
driver.get('https://python.org')

Download Comprehensive Selenium Examples Here

If you’d like to proceed to the next step or revert back, use the following navigation links:

Back | Next





Leave a Reply: