无法在selenium网格上触发selenium脚本,在本地工作

问题描述 投票:0回答:1

我正在通过 Selenium Grid 运行 selenium python 脚本,但它抛出错误

selenium.common.exceptions.WebDriverException:消息:无法找到资源,url 中的路径无效。”

下面是Webdriver安装文件

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()

这是我的测试脚本

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()

硒网格:4.22.0 Python:3.10

我已经在本地运行了这个案例,但无法在硒网格上触发,抛出错误

selenium.common.exceptions.WebDriverException: Message: Unable to find resource, invalid path in url.

python selenium-webdriver selenium-chromedriver cross-browser selenium-grid
1个回答
0
投票

这是因为

http://localhost:4444/ui
指向的是selenium网格的Web界面,而不是建立selenium会话所需的路径。因此,您需要从上述网址中删除
/ui

grid_url = "http://localhost:4444/"
webdriver_setup = WebDriverSetup(grid_url)
© www.soinside.com 2019 - 2024. All rights reserved.