C# Selenium中Click()的行为不一致

C# Selenium中Click()的行为不一致,c#,asp.net,selenium,nunit,C#,Asp.net,Selenium,Nunit,我正在开发一个产品自动化(Web CMS),其中元素。Click()显示不一致的行为。基本上我们用的是 Selenium+Nunit GUI(单元测试框架)-在特定环境上从本地运行测试用例 Selenium+Asp.net web应用程序-多个用户可以在不同的环境中运行测试用例 这里我指的是不同级别的环境(开发、SIT、QA、生产) 我的担忧 在我的一个测试用例中,我想单击一个按钮。为此,我尝试了一些代码。但所有这些都是不一致的行为。在这里,我的意思是,我为点击一个按钮而编写的代码只在我的本地或

我正在开发一个产品自动化(Web CMS),其中
元素。Click()
显示不一致的行为。基本上我们用的是

Selenium+Nunit GUI(单元测试框架)-在特定环境上从本地运行测试用例

Selenium+Asp.net web应用程序-多个用户可以在不同的环境中运行测试用例 这里我指的是不同级别的环境(开发、SIT、QA、生产)

我的担忧 在我的一个测试用例中,我想单击一个按钮。为此,我尝试了一些代码。但所有这些都是不一致的行为。在这里,我的意思是,我为点击一个按钮而编写的代码只在我的本地或服务器上工作,反之亦然

第一次尝试:- 我试了所有元素定位器的

IWebElement element = driver.FindElement(By.Id("element id goes here"))
Working fine at my local, but not in server
结果-失败

第二次尝试:-

在服务器上工作正常,但在本地 结果-失败

第三次尝试:-

无法同时在(本地和服务器)中工作 结果-失败

最后,我尝试等待元素可见并执行操作

第四次尝试:-


webdriver等待后,对该元素执行操作
(element.click())
在本地工作正常,但在服务器上工作不正常 结果-失败

我正在寻找一种解决方案,点击按钮不应该是一种不一致的行为。基本上,它在(本地和服务器)中都可以正常工作。非常感谢您的帮助..提前感谢
仅供参考-我在Mozilla Firefox浏览器38.5.2中进行测试,我在C#中使用Selenium,在Win7上本地使用,在Win10和MacOS上远程使用,并且注意到Firefox有时需要对IWebElement进行特殊处理。单击()。因此,我为自己编写了一个扩展方法,无论元素是通过什么定位器找到的,它对我来说都很好:

public static void Click(this IWebElement element, TestTarget target)
{
    if (target.IsFirefox)
    {
        var actions = new Actions(target.Driver);
        actions.MoveToElement(element);

        // special workaround for the FirefoxDriver
        // otherwise sometimes Exception: "Cannot press more then one button or an already pressed button"
        target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.Zero);
        // temporarily disable implicit wait
        actions.Release().Build().Perform();
        target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(MyDefaultTimeoutInSeconds));

        Thread.Sleep(500);
        actions.MoveToElement(element);
        actions.Click().Build().Perform();
    }
    else
    {
        element.Click();
    }
}

如果您希望测试用例具有更稳定的行为,我建议您使用ChromeDriver。它根本不需要任何特殊处理,而且速度也比FirefoxDriver快得多。

你从来没有说过问题出在哪里。“失败”不是问题的好描述。而是说发生了什么。有例外吗?发生了什么?我在C#中使用Selenium,在Win7上本地使用,在Win10和MacOS上使用Firefox浏览器@Sriram远程使用-实际上,当单击特定按钮时,会显示Firefox弹出窗口阻止程序警报消息,并且它停止了执行测试用例。仅供参考-即使弹出窗口阻止程序在浏览器级别@sriram被禁用-也不会引发异常。由于弹出窗口阻止程序窗口在浏览器级别出现,它无法执行异常操作!没错。您必须将
actions.Release().Build().Perform()放在行中进入
尝试
<代码>捕获{}
(忽略任何异常)
IWebElement element = driver.findElement(By.id("something"));
    IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
                        executor.ExecuteScript("arguments[0].click()", element);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
                return wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("element xpath goes here")));
public static void Click(this IWebElement element, TestTarget target)
{
    if (target.IsFirefox)
    {
        var actions = new Actions(target.Driver);
        actions.MoveToElement(element);

        // special workaround for the FirefoxDriver
        // otherwise sometimes Exception: "Cannot press more then one button or an already pressed button"
        target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.Zero);
        // temporarily disable implicit wait
        actions.Release().Build().Perform();
        target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(MyDefaultTimeoutInSeconds));

        Thread.Sleep(500);
        actions.MoveToElement(element);
        actions.Click().Build().Perform();
    }
    else
    {
        element.Click();
    }
}