【问题标题】:cannot catch stale element reference - Coffee script无法捕获过时的元素引用 - 咖啡脚本
【发布时间】:2015-01-28 15:14:00
【问题描述】:

我在使用下面的代码时不断收到stale element reference exception,所以我决定添加一个try/catch 块。我仍然收到此错误。我的try/catch 块没有正确写入吗?

  it 'should test cells updated correctly', ->
    try 
      element(By.css('#D6'))
      console.log('try')
    catch staleElementException
      console.log('catch')


    element(By.css('#D6')).click().then ->
      expect(element(By.css('div.gc-formula-input')).getText()).toBe 'hello'

【问题讨论】:

  • 所以,如果我错了,请纠正我 - 你知道为什么会有 staleElementException 被抛出并且你可以接受它,你只是想压制它。
  • 我知道为什么,我不想压制它,我想继续尝试找到元素,直到不再收到异常为止。如果我将 trycatch 块更改为 1 秒等待,我永远不会收到异常,但我不希望显式等待

标签: selenium coffeescript try-catch protractor e2e-testing


【解决方案1】:

将 try/catch 块放入循环中并等待它停止抛出异常。然后点击元素。

这是我第一次使用咖啡脚本和量角器,请多多包涵。

it 'should test cells updated correctly', ->

    // Define a function to check if an element is stale
    elementIsStale = (elementToCheck) ->
            try
                // Silently exercise the element
                elementToCheck.getText
                false
            catch staleElementException
                true

    // Wait while the element is stale
    while elementIsStale(element(By.css('#D6')))
        // wait a moment - don't know how to do this in coffeescript

    // Now we're ready to click on the element
    element(By.css('#D6')).click().then ->
        expect(element(By.css('div.gc-formula-input')).getText()).toBe 'hello'

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 2012-08-13
    • 2014-11-04
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多