【问题标题】:How to wait for an element before clicking on the button如何在单击按钮之前等待元素
【发布时间】:2021-05-12 19:34:53
【问题描述】:

我有一个 3 点按钮,单击该按钮可配置网格上的元素。如果我一步一步通过它很好地通过它。当我调试我的脚本时,它在网格上看不到 3 点配置按钮。因为它走得太快了。

[Then(@ "user clicks on the configure for client buttons for main flow")] 
public void ThenUserClicksOnTheConfigureForClientButtonsForMainFlow() 
{
  driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
  IWebElement waitDots = new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementExists((By.CssSelector("#grid > div.k-grid-content.k-auto-scrollable > table > tbody > tr:nth-child(3) > td:nth-child(4) > a"))));
  Actions action = new Actions(driver);
  action.MoveToElement(driver.FindElement(By.CssSelector("#grid > div.k-grid-content.k-auto-scrollable > table > tbody > tr:nth-child(3) > td:nth-child(4) > a > i"))).Build().Perform();
  IWebElement ConfigureDots = driver.FindElement(By.CssSelector("#grid > div.k-grid-content.k-auto-scrollable > table > tbody > tr:nth-child(3) > td:nth-child(4) > a > i"));
  ConfigureDots.Click();
}

【问题讨论】:

  • 请edit您的问题包含完整的错误消息和堆栈跟踪,以及您尝试与之交互的代表性 HTML 数量。

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


【解决方案1】:

简单的方法是插入一个 Thread.Sleep(),通常 1 到几秒钟就可以了。 “正确”的方法是像你所做的那样调用 WebDriverWait(),除非你可能指定了错误的条件,而且很难找到一个真正有效的 WebDriverWait() 条件。

顺便说一句,您向我们展示的代码看起来不像是在您的测试开始时开始的。只是一个警告,有时您的等待需要比您想象的更早,例如在调用此方法之前运行的代码中。

【讨论】:

    【解决方案2】:

    我已经尝试过 Thread.Sleep(),但它似乎无法正常工作,因为它仍然告诉我在网格上找不到按钮。我已经使用了 Thread.Sleep(5) 并进行了调试,它看起来并没有等待 5 秒,它只是快速通过了。

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2022-11-16
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多