Documentation

    Amazon Web Services

    Microsoft Azure

    RapidAPI

    APILayer

Last Update: 2024-11-18

Playwright on Headless Ubuntu


Table of Content


Launch Playwright on Headless Ubuntu
Connect to your VM via SSH
Deploy files on your VM remotely
Run our sample code and develop your own scripts
Run Scripts in the Background After Closing the SSH Window

Launch Playwright on Headless Ubuntu

Navigate to our listings page Playwright on Headless Ubuntu on Azure Marketplace.

Click on "Get it now" to initiate the launch dialog.

On the "Create a virtual machine" page, enter the username for the virtual machine (e.g., "azureuser"). Set up an SSH key file or use password authentication.

Review the settings and proceed to create the virtual machine.

Connect to your VM via SSH


Please refer to the official documentation for detailed instructions on how to connect to a virtual machine with SSH.

Open a Command Prompt / Terminal window and navigate to the directory where you have saved the .pem key file, which was created in the Azure portal. You can connect to your VM using the username specified during the virtual machine creation process by executing the following command:

ssh -i <name-of-key-file.pem> <username>@<public-ip>

Deploy files on your VM remotely

Download and install WinSCP or any other SCP client of your choice.

Choose 'SFTP' as the file protocol and enter the public IP address of your instance, along with the username you specified when launching the virtual machine.

If you opted for password authentication, enter the specified password. Alternatively, if you wish to use a private key file (.pem) that you generated, you can add it by first clicking 'Advanced' and then selecting 'Authentication' from the left-side menu.

Connect to the virtual machine by clicking on 'Login'.

Run our sample code and develop your own scripts

Connect to the terminal via SSH and log in with the user you specified when you created the VM in Azure.

Navigate to the /opt/playwright/nodejs directory by entering:

cd /opt/playwright/nodejs

Install newest browser versions with:

npx playwright install

Run our sample code with the command:

node script.js

After a successful run, you will see:

Note: The first execution after startup is very slow, especially on VMs with low memory configured, as the browser can run out of memory. Performance improves over time.

You can find the taken screenshots by using the command:

ls

Edit files by entering:

nano script.js

Alternatively, you can deploy your scripts with an SCP tool. You will find demo code for all available browsers in Playwright within the script.

Run the modified scripts again with the command:

node script.js
Run Scripts in the Background After Closing the SSH Window


Connect to the terminal via SSH by logging in with your created user account (the default username is 'azureuser'). For guidance, refer to the chapter titled 'Connect to Your VM via SSH'.

Start a new tmux session by entering the following command:

tmux

Start the script you wish to run in the background.

To detach the tmux session while keeping your script running in the background, press CTRL+b followed by d. You can now safely close the SSH session; your script will continue to run inside tmux.

To resume your tmux session at any time, use the command:

tmux attach