首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在使用Ruby的Selenium中,什么是Java的elementToBeClickable等价物?

在使用Ruby的Selenium中,什么是Java的elementToBeClickable等价物?
EN

Stack Overflow用户
提问于 2018-10-18 05:03:26
回答 2查看 288关注 0票数 1

我刚接触自动化框架的Ruby脚本。我正在尝试将Selenium和Ruby结合起来作为自动化框架。我发现的是,Ruby selenium中没有elementToBeClickable?我有一个脚本,我使用常规的单击方法在元素上单击。但是,如果单击不起作用,元素就不会被单击。所以我正在考虑等待元素变得可点击,但是Selenium的ruby库没有像Java和C#中那样的方法。你们是如何等待元素在Ruby中可点击的呢?

  def click_more_actions_button
    more_actions_id = 'btnMoreActions'
    el = @wd.find_element(id: more_actions_id)
    el.click
  end
EN

回答 2

Stack Overflow用户

发布于 2018-10-18 05:17:23

wait.until(ExpectedConditions.elementToBeClickable (By.id("foo")));

下面的几行完全等同于上面的java代码,

wait.until { driver.find_element(id: "foo").enabled? }

事实上,java中的elementToBeClickable方法是有误导性的,实际上它会检查元素的启用状态。

参考ExpectedContions.java中elementToBeClickable的实现。

  /**
   * An expectation for checking an element is visible and enabled such that you can click it.
   *
   * @param locator used to find the element
   * @return the WebElement once it is located and clickable (visible and enabled)
   */
  public static ExpectedCondition<WebElement> elementToBeClickable(final By locator) {
    return new ExpectedCondition<WebElement>() {
      @Override
      public WebElement apply(WebDriver driver) {
        WebElement element = visibilityOfElementLocated(locator).apply(driver);
        try {
          if (element != null && element.isEnabled()) {
            return element;
          }
          return null;
        } catch (StaleElementReferenceException e) {
          return null;
        }
      }

      @Override
      public String toString() {
        return "element to be clickable: " + locator;
      }
    };
  }

请参阅github中的this

票数 0
EN

Stack Overflow用户

发布于 2020-04-24 12:28:27

实际上,Ruby编程语言中并没有这种直接的方法。

以下是另一种选择,即检查并等待,直到元素显示并启用以对其执行操作,如单击、send_keys等:

wait = Selenium::WebDriver::Wait.new(:timeout => 10)
element = wait.until{
   tmp_element = driver.find_element(:name, 'password')
   tmp_element if tmp_element.enabled? && tmp_element.displayed?
}

使用wait.until,我们可以做到这一点。

希望能对你有所帮助。如果您有任何问题,请随时提出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52863582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档