A reusable, object-oriented way of automating a browser

Image for post

Photo: Safar Safarov, Unsplash

If you are a complete beginner, I strongly urge you to check out part 1 of this tutorial first. The beginning of this tutorial will be geared toward beginners, so I will be covering Python class and method basics, but it is only going to cover the bare basics. You can find the TL;DR right below it.

Why not reuse the code from part 1?

As you may have noticed, there is a lot of manual input in the previous process. We would need to copy-paste a lot of code into a new Python file or retype it all. A rule of thumb I use: if you ever find yourself needing to duplicate code, try to automate the process as much as possible.


Classes and Methods

In order to automate, it is crucial to understand what classes and methods are. Let me give you an example: Say we want to be able to create ‘dogs’ in your code easily and efficiently. We need a dog class — a blueprint that will contain the essential characteristics that a dog usually has. Within the dog class we first define our constructor, which is where we define all attributes and objects that belongs to all dogs we create (this isn’t exactly what a constructor method does but for an initial conceptual understanding we will work off this). The constructor (in Python it appears as init) is a **method, **which is something that executes within classes. Methods are not restricted to constructors; a method is just an umbrella term for specific functionality that our class can perform.

Our init method takes all the default arguments that we feel ALL dogs must be given, which in this case, are breed and name. Our bark method takes our passed name and species, and prints a sentence given those attributes.Here is a heavily commented example:

Just a recap…

  • The **class **is a blueprint of a Dog
  • The** init** **constructor **inside the class defines all of the attributes that belong to ALL methods of that class.
  • The bark method, which takes the attributes of the init method only (so the breed and the name), prints out the class dog name and class dog breed when called.

Again, this is very simplified, but it’s the view you’ll need to understand the code we write for headless browsing.


TL;DR — Just Give Me the Code

Locator Class

Navigator Class

Utilization

#selenium #python #software-development #browser-automation

Automated Browsers, Scraping and Crawling — Part 2
1.10 GEEK