【问题标题】:click() function on selenium not working in C#硒上的click()函数在C#中不起作用
【发布时间】:2016-05-17 11:42:49
【问题描述】:

我真的厌倦了这个问题,没有找到任何解决方案。

问题是我正在尝试使用 C# 中的测试自动化来测试网站。为此我需要登录,但登录按钮实际上并不是一个按钮,它是一个带有属性集 'role=button' 的 span 标签。

我将 Selenium 与 Chrome 网络驱动程序一起使用,并使用单元扩展来自动化测试。问题是每当我在测试资源管理器中运行测试时,都会执行单击事件,但页面不会导航到下一页,并且什么也没有发生。但是,当我设置断点并通过main 函数运行相同的测试时,它工作正常。

我必须设置断点才能执行点击操作和登录操作。

我正在使用 Visual Studio 控制台应用程序。这是pic of my web site html tag

【问题讨论】:

  • 请格式化您的答案以使其更具可读性并提供一些代码。
  • 简单按钮上的简单 click() 函数不会出现问题

标签: c# selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

尝试通过JavascriptExecutor点击。

JavascriptExecutor 是 Selenium Webdriver 提供的接口

WebElement element= driver.findElement(By."Your Locator"))
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

这是一个java代码,你可以参考下面的链接修改它:-

Execute JavaScript using Selenium WebDriver in C#

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    问题很可能是在您“快速”运行测试时未加载 Click 函数,但在您调试时。 尝试加载页面。等待 1 秒。然后 getElement 并单击它。 这在动态添加 javascript 函数到元素的页面中是正常的。

    【讨论】:

      【解决方案3】:

      据我了解,点击是在页面完全加载之前执行的。尝试使用显式等待和expected conditins

      WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
      wait.Until(ExpectedConditions.ElementToBeClickable(logInButton)).Click();
      

      这将等待最多 10 秒,以使按钮在单击之前可单击。

      【讨论】:

        【解决方案4】:

        我通过将元素存储在 IWebElement 类型变量中然后单击它来解决此问题。

        IWebElement runButton = driverIE.FindElement(By.XPath("//*your XPath"));
        runButton.Click();
        

        【讨论】:

          【解决方案5】:

          如果您的项目中包含Selenium.Support,您可以执行以下操作

          Driver.ExecuteJavaScript("arguments[0].click();", element);
          

          ExecuteJavaScript 是库中可用的扩展方法

          【讨论】:

            猜你喜欢
            • 2014-11-01
            • 2021-01-10
            • 1970-01-01
            • 2015-02-18
            • 2022-01-07
            • 2021-08-07
            • 2018-05-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多