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

SpecFlow中仓库类的命名XPath在其他类调用FindElement时无法识别

问题解决:"AcceptCookies"在当前上下文中不存在

原因分析

你定义的AcceptCookies是Repository类的静态成员,即便导入了对应命名空间,也无法直接通过成员名访问——静态成员必须通过所属类名限定调用,或通过using static指令简化访问逻辑。

解决方案

方案1:通过类名直接访问静态成员

在步骤定义代码中,用Repository类名限定静态成员即可:

[Given(@"Navigate to morrisons")]
public void GivenNavigateToMorrisons()
{
    driver.Url = "https://morrisons.com";
    // 通过类名访问静态成员
    driver.FindElement(Repository.AcceptCookies).Click(); 
}

方案2:使用using static简化调用

在步骤定义文件的顶部using区域添加using static指令,之后就能直接使用静态成员名:

// 先确保已导入命名空间
using SpecFlowProject1.Repository;
// 添加using static指令,直接引用类的静态成员
using static SpecFlowProject1.Repository.Repository;

[Given(@"Navigate to morrisons")]
public void GivenNavigateToMorrisons()
{
    driver.Url = "https://morrisons.com";
    // 直接使用成员名
    driver.FindElement(AcceptCookies).Click(); 
}

额外验证

确认步骤定义文件的using区域已正确导入命名空间:

using SpecFlowProject1.Repository;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 22:25:00