Categories We Write About

Build a simple browser automation bot

Here’s a simple browser automation bot using Python with Selenium, a popular tool for browser automation tasks such as navigating websites, clicking buttons, and filling out forms:


Prerequisites

  • Install Python 3

  • Install the required libraries:

bash
pip install selenium
  • Download the appropriate ChromeDriver for your Chrome browser version and add it to your system PATH or specify the path in the script.


Python Code: Simple Browser Automation Bot

python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time # Set up the Chrome driver driver = webdriver.Chrome() # Or specify path: webdriver.Chrome(executable_path="path/to/chromedriver") # Open a website driver.get("https://www.google.com") # Wait for page to load time.sleep(2) # Find the search box, enter a query, and press Enter search_box = driver.find_element(By.NAME, "q") search_box.send_keys("OpenAI ChatGPT") search_box.send_keys(Keys.RETURN) # Wait for results to load time.sleep(2) # Click on the first search result first_result = driver.find_element(By.CSS_SELECTOR, 'h3') first_result.click() # Wait for a while to observe the result time.sleep(5) # Close the browser driver.quit()

What It Does

  1. Opens Google in Chrome.

  2. Searches for “OpenAI ChatGPT”.

  3. Clicks on the first result.

  4. Waits 5 seconds and closes the browser.


Notes

  • You can extend this to handle form submissions, login automation, scraping content, etc.

  • Make sure to handle exceptions for more robust bots.

  • Respect website terms of service and avoid spamming or scraping sites that disallow automation.

Would you like this converted into another language (like JavaScript with Puppeteer) or with headless mode enabled?

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

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

Categories We Write About