Remote WebDriver Setup for Browser Automation with Selenium
Written on
Chapter 1: Introduction to Remote WebDriver
In the previous article, we explored the integration of Selenium with the unittest framework. In this concluding installment, we will delve into utilizing WebDriver on a remote server.
Previously, our automation scripts were executed on local machines. However, WebDriver can also operate remotely, allowing you to keep your scripts on your local computer while executing them on a separate remote machine.
A remote WebDriver consists of two main components: the client, which is your code, and the server, a Java servlet.
The architecture overview previously discussed various types of remote WebDriver configurations, which can be summarized as follows:
Let's proceed with a practical example.
Section 1.1: Setting Up the Virtual Machine
For this demonstration, I installed Xubuntu on VirtualBox.
Steps for Configuration:
- Create a virtual machine with a minimum of 2 CPUs to prevent errors during the Selenium server execution.
- Configure the network adapter to operate in bridged mode.
- Record the IP address of the machine, which will be used in your script.
- Download the necessary browser WebDriver (in this case, geckodriver) and place it under /usr/local/bin or another location in your system's PATH. More installation details can be found in the previous post.
- Obtain the Selenium server JAR file.
Section 1.2: Running the Selenium Server
On the virtual machine (remote server), open two terminals and execute the following commands for both the hub and node, using the version you downloaded:
java -jar selenium-server-4.0.0-beta-3.jar hub
This command will initiate the Selenium Hub, as indicated by the following log output:
18:54:30.877 INFO [UnboundZmqEventBus.] - Sockets created
18:54:30.983 INFO [UnboundZmqEventBus.] - Event bus ready
18:54:33.917 INFO [Hub.execute] - Started Selenium Hub 4.0.0-beta-3 (revision 5d108f9a67): http://192.168.1.39:4444
Next, when you start the node, logs similar to the following will appear:
...java -jar selenium-server-4.0.0-beta-3.jar node
18:54:36.576 INFO [Node.] - Binding additional locator mechanisms: name, id
18:54:37.358 INFO [LocalDistributor.add] - Added node d27ae900-3309-4443-ab19-13fe7340c4e2 at http://192.168.1.39:5555.
18:54:38.082 INFO [GridModel.setAvailability] - Switching node d27ae900-3309-4443-ab19-13fe7340c4e2 (uri: http://192.168.1.39:5555) from DOWN to UP
Section 1.3: Configuring the Local Script
On your local machine, set up your script with the remote machine's IP address noted earlier. By default, port 4444 is used:
driver = webdriver.Remote(
command_executor="http://192.168.1.39:4444",
desired_capabilities=capabilities
)
Finally, execute your script with:
python remote_client_vm.py
Chapter 2: Key Takeaways
It's important to remember that you can utilize WebDriver remotely in a manner similar to local execution. A remote WebDriver is comprised of the client (your code) and the server (the Java servlet).
Steps to Execute:
On the remote machine:
- java -jar selenium-server-x.x.x.jar hub
- java -jar selenium-server-x.x.x.jar node
On the local machine:
- Configure your script using the command_executor parameter.
- python your_script.py
I've gained significant insights while studying these topics and have been sharing them for the past eight months. Thank you for following this series!
References
Description: Explore Selenium Browser Automation in Python with this video tutorial that covers the essentials of using Selenium for automated testing.
Description: Dive deeper into Advanced Python Programming for Browser Automation with Selenium in this comprehensive video guide.