I am running selenium python script through Selenium Grid, but it throws error
selenium.common.exceptions.WebDriverException: Message: Unable to find resource, invalid path in url."
Below is Webdriver setup file
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.ui import WebDriverWait
import os
class WebDriverSetup:
def __init__(self, grid_url, window_size="1920,1080", device_scale_factor="1", timeout=100):
self.grid_url = grid_url # URL of the Selenium Grid hub
self.path = r"path to driver.exe"
self.window_size = window_size
self.device_scale_factor = device_scale_factor
self.timeout = timeout
self.driver = None
self.wait = None
def start_driver(self):
# Set up Edge options
print(self.grid_url)
print(self.path)
edge_options = Options()
edge_options.add_argument(f"--force-device-scale-factor={self.device_scale_factor}")
# Set download directory preferences
download_dir = os.path.join(os.getcwd(), "downloads")
if not os.path.exists(download_dir):
os.makedirs(download_dir)
edge_options.add_experimental_option("prefs", {
"download.default_directory": download_dir,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
})
# Create a remote WebDriver instance
self.driver = webdriver.Remote(
command_executor=self.grid_url,
options=edge_options,
# desired_capabilities=edge_options.to_capabilities()
)
# Maximize the browser window
self.driver.maximize_window()
self.wait = WebDriverWait(self.driver, self.timeout)
return self.driver, self.wait
def set_timeout(self, timeout):
self.timeout = timeout
self.wait = WebDriverWait(self.driver, self.timeout)
# Example of changing timeout
# webdriver_setup.set_timeout(200) in test file
def stop_driver(self):
if self.driver:
self.driver.quit()
This is my test script
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from utilities.webdriver_setup import WebDriverSetup # Assuming your WebDriverSetup class is in webdriver_setup.py
grid_url = "http://localhost:4444/ui/"
webdriver_setup = WebDriverSetup(grid_url)
try:
driver = None
# Start WebDriver session on Selenium Grid
driver, wait = webdriver_setup.start_driver()
print("Driver started successfully")
# Example: Navigate to a website
driver.get("https://www.google.com")
print(f"Title: {driver.title}")
# Your test logic here
finally:
# Ensure driver is stopped even if an exception occurs
if driver:
webdriver_setup.stop_driver()
Selenium grid : 4.22.0 Python : 3.10
I have ran the case on local but not able to trigger on selenium grid, throws me error selenium.common.exceptions.WebDriverException: Message: Unable to find resource, invalid path in url.
.
It is because http://localhost:4444/ui
is pointing to the web interface of the selenium grid and not the path required to establish a selenium session. Hence, you'll need to remove /ui
from the mentioned url.
grid_url = "http://localhost:4444/"
webdriver_setup = WebDriverSetup(grid_url)