【问题标题】:Selenium SendKeys() and Click() silently fail after link navigationSelenium SendKeys() 和 Click() 在链接导航后静默失败
【发布时间】:2021-02-21 16:38:46
【问题描述】:

我正在为一个简单的 Web 应用程序编写自动化 UI 测试:

应用非常简单:只有服务器端逻辑,没有客户端脚本,没有 AJAX,没有客户端框架。

这是我的测试场景

  1. 使用 Selenium 打开应用主页: https://shorturl.nakov.repl.co
  2. 点击链接[添加网址]导航至:https://shorturl.nakov.repl.co/add-url
  3. https://shorturl.nakov.repl.co/add-url填写表格并提交
  4. 提交后,断言新元素已添加并在主页可见 (https://shorturl.nakov.repl.co)

除非我使用 dirty hack Thread.Sleep(500),否则表单元素的 SendKeys()Click() 命令 fail 并且表单保持为空且未提交。仅当表单由链接导航加载时才会发生这种情况。如果我通过 URL 打开它,它可以工作。

我尝试了很多等待和修复,但没有任何帮助,除了Thread.Sleep()

这个问题看起来像 Chromium 中的错误,因为相同的逻辑 在 Firefox 中可以正常工作,但在 Opera 中却以同样的方式失败和边缘

这是失败的 Selenium ChromeDriver 测试:

[Test]
public void Test_AddUrl_Chrome()
{
    // Setup the Chrome Web Driver
    using (var driver = new ChromeDriver())
    {
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);

        // Open the Web app and navigate to the "Add URL" page
        driver.Navigate().GoToUrl("https://shorturl.nakov.repl.co/");
        var addUrlLink = driver.FindElementByXPath("//header/a[text()='Add URL']");
        addUrlLink.Click();

        // Wait until the page "Add Short URL" is loaded --> does not help
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
        wait.Until(d => d.FindElement(By.XPath("//main/h1[text()='Add Short URL']")));

        // Wait until the form [submit] button becomes clickable --> does not help
        wait.Until(ExpectedConditions.ElementToBeClickable(
            By.CssSelector("form button")));

        // This wait works, but is а bad practice
        //Thread.Sleep(500);

        // Fill the form and submit it
        var newUrl = "http://newurl" + DateTime.Now.Ticks + ".com";
        var textBoxUrl = driver.FindElementByCssSelector("input.url");

        // SendKeys() does not change the element value (unless I use Thread.Sleep())
        textBoxUrl.SendKeys(newUrl);

        var buttonCreate = driver.FindElementByCssSelector("form button");
        // Click() does not submit the form (unless I use Thread.Sleep())
        buttonCreate.Click();

        // Assert the "Short URLs" page holds the new URL
        var newUrlLink = driver.FindElementByLinkText(newUrl);
        Assert.AreEqual(newUrl + "/", newUrlLink.GetAttribute("href"));
    }
}

驱动程序日志显示执行成功。没有错误,但 SendKeys()Click() 静默失败,没有错误。表单字段保持空白,未单击提交按钮。

您可以在您的机器上运行测试以确保它失败。它没有依赖关系,并且目标 Web 应用程序在 Web 上运行。

如果您在页面导航后取消注释Thread.Sleep(500),则测试成功。

我使用最新的 Chrome 88.0.4324.182(64 位,在 Windows 10 上)及其对应的 64 位 chromedriver.exe(相同版本)。

同样的测试在 FirefoxDriver 上运行良好

[Test]
public void Test_AddUrl_Firefox()
{
    // Setup the Firefox Web Driver (use ::1 for faster execution)
    var ffService = FirefoxDriverService.CreateDefaultService();
    ffService.Host = "::1";
    using (var driver = new FirefoxDriver(ffService))
    {
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);

        // Open the Web app and navigate to the "Add URL" page
        driver.Navigate().GoToUrl("https://shorturl.nakov.repl.co/");
        var addUrlLink = driver.FindElementByXPath("//header/a[text()='Add URL']");
        addUrlLink.Click();

        // Fill the form and submit it
        var newUrl = "http://newurl" + DateTime.Now.Ticks + ".com";
        var textBoxUrl = driver.FindElementByCssSelector("input.url");
        textBoxUrl.SendKeys(newUrl);
        var buttonCreate = driver.FindElementByCssSelector("form button");
        buttonCreate.Click();

        // Assert the "Short URLs" page holds the new URL
        var newUrlLink = driver.FindElementByLinkText(newUrl);
        Assert.AreEqual(newUrl + "/", newUrlLink.GetAttribute("href"));
    }
}

这是 Chromium Web 驱动程序中的错误,还是我做错了错误

【问题讨论】:

  • 我不认为这是个问题,但是<title> 标记存在于<head><body> 之外作为<html> 标记的子级. <label> 标签具有 for 属性,但输入上没有对应的 id 属性。只是出于好奇,修复 HTML 语法错误会修复 ChromeDriver 的问题吗?如有疑问,请先检查最简单的问题。
  • 感谢您的建议。我修复了该应用程序,使其所有页面都包含有效的 HTML5。这并不能解决问题。还有其他建议吗?
  • 这些是 ChromeDriver 日志:bugs.chromium.org/p/chromedriver/issues/…

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


【解决方案1】:

找到了解决方案SendKeys()Click() 静默失败:使用 JavaScript 执行类似操作。

// SendKeys() does not change the element value (unless I use Thread.Sleep())
//textBoxUrl.SendKeys(newUrl);
driver.ExecuteScript($"arguments[0].setAttribute('value', '{newUrl}')", textBoxUrl);

var buttonCreate = driver.FindElementByCssSelector("form button");
// Click() does not submit the form (unless I use Thread.Sleep())
//buttonCreate.Click();
driver.ExecuteScript($"arguments[0].click()", buttonCreate);

以上脚本直接在textBoxUrl字段中赋值value,点击buttonCreate按钮。

这仍然是解决我在 ChromeDriver 88.0.4324.96 中发现的错误的方法。

感谢 Anton Angelov (https://stackoverflow.com/users/3501502/anton-angelov) 对此问题的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    相关资源
    最近更新 更多