You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地正常的Selenium Python脚本在Grid触发失败,报URL路径错误

问题排查与解决:Selenium Grid运行时报错"Unable to find resource, invalid path in url"

核心原因分析

  1. 错误的Grid连接地址
    测试脚本中使用的grid_url = "http://localhost:4444/ui/"是Selenium Grid的Web管理UI地址,而非远程WebDriver会话的命令执行端点。WebDriver尝试向UI地址发送会话请求,导致路径无效,触发"invalid path in url"错误。

  2. 冗余的本地驱动配置
    WebDriverSetup类中定义的self.path(本地driver.exe路径)在RemoteWebDriver场景下完全无效——Selenium Grid的节点会自行管理浏览器驱动,无需本地指定驱动路径。

解决方法

1. 修正Grid连接URL

将测试脚本中的Grid地址替换为正确的远程命令端点,Selenium 4.x的标准端点为http://localhost:4444/wd/hub:

grid_url = "http://localhost:4444/wd/hub"

部分新的Selenium Grid版本也支持直接使用根路径http://localhost:4444,若上述地址仍报错可尝试此选项。

2. 移除冗余的本地驱动配置

修改WebDriverSetup类,删除无用的self.path相关代码:

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

3. 额外检查项

  • 确认Selenium Grid Hub和节点已正常启动,可通过http://localhost:4444/ui查看节点是否成功注册。
  • 确保Grid节点上安装的Edge浏览器版本与EdgeDriver版本完全匹配,避免版本不兼容问题。

内容的提问来源于stack exchange,提问作者Rishabh Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 20:00:05