Python Browser Controller

First, let’s install the necessary packages: `pip install selenium` and `pip install webdriver-manager`. These will allow us to interact with our favorite browsers using Python code.
Now that we have those installed, let’s create a new file called `browser_controller.py` (or whatever you want to call it) in your preferred text editor or IDE.
Inside this file, add the following lines of code:

# Import necessary modules
from selenium import webdriver # Importing the webdriver module from the selenium library
from selenium.webdriver.common.keys import Keys # Importing the Keys module from the selenium.webdriver.common library
import time # Importing the time module

# Set up browser and navigate to desired website
browser = webdriver.Firefox() # Creating a new instance of the Firefox webdriver and assigning it to the variable 'browser'
browser.get("https://www.google.com") # Using the 'get' method to navigate to the desired website
time.sleep(2) # Using the 'sleep' method from the time module to pause the script for 2 seconds before continuing

This code sets up a Firefox browser using the `webdriver.Firefox()` function, navigates to Google’s homepage, and waits for two seconds (using the `time.sleep()` function). You can replace “https://www.google.com” with any website you want to visit!
Now let’s add some more functionality: how about we search for something on Google?
Add this code below the previous lines:

# Import necessary libraries
import time # Import the time library to use the sleep function
from selenium import webdriver # Import the webdriver from selenium library
from selenium.webdriver.common.keys import Keys # Import the Keys class from selenium library

# Create a new instance of the Chrome driver
browser = webdriver.Chrome()

# Navigate to Google's homepage
browser.get("https://www.google.com")

# Wait for 2 seconds before continuing
time.sleep(2)

# Find and click the search bar input field
search_bar = browser.find_element_by_name("q") # Use find_element_by_name method to locate the search bar by its name attribute
# Note: "q" is the name attribute of the search bar on Google's homepage

# Wait for 1 second before continuing
time.sleep(1)

# Type in our desired query
search_bar.send_keys("Python Browser Controller Tutorial") # Use send_keys method to type in the desired query in the search bar

# Wait for 2 seconds before continuing
time.sleep(2)

# Press the Enter key to submit search
search_bar.send_keys(Keys.RETURN) # Use send_keys method to simulate pressing the Enter key to submit the search


This code finds the input field on Google’s homepage (using `find_element_by_name(“q”)`) and waits for one second before typing in our desired query (“Python Browser Controller Tutorial”). After that, it presses Enter to submit the search.
But what if we want to click on a link instead? Let’s say we found an article about Python browser control and want to read it in full:
Add this code below the previous lines:

# This script is used to automate the process of searching for a specific query and opening a link in a new tab using a Python browser controller.

# Import necessary libraries
import time # Import the time library to add delays
from selenium import webdriver # Import the Selenium library for browser automation

# Create a new instance of the Chrome driver
browser = webdriver.Chrome()

# Navigate to Google
browser.get("https://www.google.com/")

# Find the search bar and input our desired query
search_bar = browser.find_element_by_name("q")
search_bar.send_keys("Python Browser Controller Tutorial")

# Press Enter to submit the search
search_bar.send_keys(Keys.ENTER)

# Wait for 1 second before continuing
time.sleep(1)

# Find the first result (assuming there is only one)
result = browser.find_elements_by_class_name("g")[0]

# Click on the link to open it in a new tab
link = result.find_element_by_tag_name("a").click()

# Open a new tab using JavaScript
browser.execute_script("window.open('');")

# Wait for 2 seconds before continuing
time.sleep(2)

# Close the current tab and switch to the newly opened tab
browser.close()
browser.switch_to.window(browser.window_handles[0])

# Now we can read the article about Python browser control in full!

This code finds the first search result (assuming there is only one), waits for one second, and clicks on its link to open it in a new tab using JavaScript. The `execute_script()` function allows us to execute arbitrary JavaScript code within our Python script!
And that’s it! You now have the basic knowledge of how to use Python browser control to automate tasks and save time.

SICORPS