How to Automate Your Browser with Python and Selenium

ASHISH GUPTA

ASHISH GUPTA

5 min read

  • python
AutomationWithSeleniumBanner

Introduction

Are you looking to automate your browser that helps to do repetitive work on browser, Then python helps you a lot, Using Selenium you can automate your browser, Selenium is an open-source tool that allows you to automate web browsers. It provides a way to restoring user interactions with web pages, it can be use for form filling where the form or user data can extract from excel file and put data on website automatically, it makes useful for tasks such as testing websites, dispose of data, and performing routine tasks.

requirement

  • Python is not installed on your system. You can download it from python.org.

  • Basic knowledge of Python programming.

  • A web browser (Google Chrome, Firefox, etc.).

Installing Selenium

To use Selenium with Python, you need to install the Selenium package. You can do this using pip in your editor terminal or in power-shell also :

pip install selenium

Setting Up the WebDriver

Selenium requires a WebDriver to interface with the browser. Depending on the browser you want to automate, download the appropriate WebDriver:

  • Google Chrome: ChromeDriver

  • Mozilla Firefox: GeckoDriver

Ensure the WebDriver executable is in your system PATH or specify its location in your script.

Basic Browser Automation

Here is a simple example to get you started with automating a browser using Selenium and Python:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# Set up the WebDriver (make sure the path to chromedriver is correct)
driver = webdriver.Chrome()

# Open a website
driver.get('https://www.google.com')

# Find the search box using its name attribute value
search_box = driver.find_element(By.NAME, 'q')

# Type a search query
search_box.send_keys('BiyondBytes.com')

# Press Enter
search_box.send_keys(Keys.RETURN)

# Wait for a few seconds to let the results load
time.sleep(5)

# Close the browser
driver.quit()

Common Selenium Commands

  • Finding Elements:

element = driver.find_element(By.ID, 'element_id')
element = driver.find_element(By.CLASS_NAME, 'class_name')
element = driver.find_element(By.XPATH, '//tag[@attribute="value"]')

driver.find_element(By.ID,'element _id') this lines helps to find the input box of specific field like email and password field by element_id if you are familiar with HTML you good to know what is this.

  • Interactions:

element.click()  # Click an element
element.send_keys('text')  # Type text into an element

Above code helps you to click the button using his id or type of the button means you store attribute value in the variable then you apply interactions like click event,

send_keys is use you send text or something else in input field dynamically.

  • Navigation:

driver.get('https://biyondbytes.com')  # Navigate to a URL it's an example what you can use your urls
driver.back()  # Go back to the previous page
driver.forward()  # Go forward to the next page

If you are familiar with react or next js you probably know that what is navigation using navigation like forward() and bac() use for undo the page or redo the page.

  • Waiting:

driver.implicitly_wait(10)  # Implicitly wait for 10 seconds
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, 'element_id'))
)

Example: Automating a Login

Here’s an example of how to automate logging into a website:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
import time
from faker import Faker 
# Set up the driver (Make sure to replace 'path/to/your/chromedriver' with the actual path)

# Navigate to the URL containing the form
name = input("Enter the username for  the form: ")
for i in range(1, 100):
    try:
        driver = webdriver.Firefox()
        driver.get('https://smmpanel.com/signup')
        # Wait until the form is present
        form = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, 'signup-frm'))
        )

        # Fill in the form fields
        driver.find_element(By.ID, 'login').send_keys(f'youbd{name}v{i}')
        driver.find_element(By.ID, 'email').send_keys(f'y{name}{i}il@kaku.com')
        driver.find_element(By.ID, 'first_name').send_keys('YourFirstNrame')
        driver.find_element(By.ID, 'last_name').send_keys('YourLastNamer')
        driver.find_element(By.ID, 'whatsapp').send_keys('1234567890')
        driver.find_element(By.ID, 'skype').send_keys('your_skype')
        driver.find_element(By.ID, 'telegram').send_keys('your_telegram')
        driver.find_element(By.ID, 'password').send_keys('your_password')
        driver.find_element(By.ID, 'password_again').send_keys('your_password')

        # Check the Terms of Service checkbox
        terms_checkbox = driver.find_element(By.NAME, 'RegistrationForm[termsofservice]')
        if not terms_checkbox.is_selected():
            terms_checkbox.click()

        # time.sleep(30)
        isCaptcha = True
        while isCaptcha:
            recaptcha_response = driver.find_element(By.ID, "g-recaptcha-response")
            print("please verify captcha",recaptcha_response.get_attribute("value"))
            if(recaptcha_response.get_attribute("value")):
                isCaptcha = False
                break

        # # Submit the form
        time.sleep(2)
        print("submitting form........")
        submit_button = driver.find_element(By.XPATH, "//button[@type='submit']")
        submit_button.click()
        time.sleep(4)
        dropdown = Select(driver.find_element(By.ID, "orderform-category"))

        # Select an option by its value
        dropdown.select_by_value("20379")
        driver.find_element(By.ID, 'field-orderform-fields-link').send_keys('https://www.instagram.com/username')
        driver.find_element(By.ID, 'field-orderform-fields-quantity').send_keys(49)
        submit_button = driver.find_element(By.XPATH, "//button[@type='submit']")
        submit_button.click()
        # # Optionally, wait for some response or element after submission
        # WebDriverWait(driver, 10).until(
        #     EC.presence_of_element_located((By.CLASS_NAME, 'some-class-after-submit'))
        # )

    finally:
    # Close the driver
        driver.quit()
        print("Form submitted successfully")

Above written code is Mine project for increase IG Followers i automate this website for registering multiple times and increase follower or user on your IG.

Conclusion

Using Python selenium you automate your work like applying forms on website integrate excel sheet to map your user details with website for registering.

I Have a project for you, Make a project taking details of user using google docs and save details in excel then map your user details with the form fields of your website. Automating web browsers with Python and Selenium can greatly enhance your productivity by automating repetitive tasks. This guide has covered the basics to get you started, but Selenium is a powerful tool with many advanced features. Explore the official Selenium documentation for more detailed information and advanced usage.

Hit the like button and share this blog because IG follower increase has paid tools and i gave you source code here for free. just do it.

Happy Automating!

ASHISH GUPTA

ASHISH GUPTA

Creative, Elegant and Visionary

Latest

from the blog

The latest industry news,interviews and resources

Python's One-Line if else Magic

Python's One-Line if else Magic

Python's One-Line if else Magic

Best Alternatives to Adobe's Generative Fill AI Free

Best Alternatives to Adobe's Generative Fill AI Free

Fill for free online which enhance and boost your design works

Join our newsletter

we'll send you a nice letter once per week. No spam.

BiyondBytes

© 2024 BiyondBytes. All rights reserved.