Creating a New Selenium Script

Before you start, make sure you have Python and pip installed on your system. If you don’t have them yet, you can download them from the official Python website and the official Pip website.

With Python and pip installed, you can create a new Selenium script. First, install Selenium using pip:

pip install selenium

Next, create a new Python file (for example, main.py) and add the following code to create a basic Selenium script:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

# Define options for Chromium
options = Options()
options.add_argument("--headless")  # Run in the background. This means that the browser runs without opening a browser window.
options.add_argument("--no-sandbox")  # Required when you are running as root. This allows Selenium to start in a user-unsupported environment.
options.add_argument("--disable-dev-shm-usage")  # This overcomes limitations when dealing with elements on large pages.

# Initialize the driver
service = Service('/usr/bin/chromedriver') # On Square Cloud, the path to the Chromium driver is '/usr/bin/chromedriver'
driver = webdriver.Chrome(service=service, options=options)

# Set the window size to larger dimensions
driver.set_window_size(1920, 1080)  # Adjust these values as needed

# Open the web page
driver.get('https://www.google.com')

# Loop to take a print of the page every 1 minute
while True:
    driver.save_screenshot('page.png')
    print("Page print saved.")
    time.sleep(60)  # Pause for 60 seconds

Managing Dependencies with the requirements.txt file

The requirements.txt file is a file that lists all the external libraries needed for your project. You should list all the libraries that your project will use, excluding native libraries like random and datetime. This file should be included when submitting your application to Square Cloud.

requirements.txt
selenium

About binary_location

In the provided example, binary_location is not necessary because the Selenium WebDriver tries to find the browser on its default path if binary_location is not specified. This means that if Chromium is installed in a location that is in the system PATH, the WebDriver can find it automatically without you having to specify the location.

In the case of Square Cloud, if the Chromium version is fixed and cannot be changed, and the Selenium WebDriver can automatically find the Chromium executable, then you would not need to specify the binary_location. However, if there is any reason why the WebDriver cannot find Chromium automatically, you would need to use binary_location to specify the location of the executable.

Creating the squarecloud config file

Learn about: how to make the configuration file for Square Cloud.

The squarecloud.app file is a configuration file that will be used to configure your application; it will be used to define the name, description, version, main file, among other things.

Uploading your project to Square Cloud

After following all the steps, package your application files into a zip file where the configuration file is and upload the site on Upload. Remember to include the requirements.txt file, but do not include files like __pycache__. You can get more information about unnecessary files when hosting at automatic-deletion-files-when-deploying-an-application-to-squarecloud.

Additional Resources

For more information about Selenium and its tools, visit the official Selenium documentation. There, you’ll find detailed guides, tutorials, and API documentation to help you make the most out of Selenium.

If you continue to experience any issues, please don’t hesitate to contact our support team.