【问题标题】:Watir can't find element in IE and PhantomJSWatir 在 IE 和 PhantomJS 中找不到元素
【发布时间】:2014-10-02 03:55:06
【问题描述】:

我在不同浏览器中运行脚本时遇到问题:我的脚本在 Chrome、Firefox 中运行正常,但在 IE 和 PhantomJS 中存在问题。最奇怪的是几天前我在 PhantomJS 中成功完成了相同的脚本,我确信没有人更改 gems/浏览器。 这是我要执行的脚本:

class HomePage
   ....
   link(:signInLink, :text=>"Sign In")
   ....
def log_in(username="admin", password="admin123")
   self.signInLink_element.when_present(60).click//hangs here
   self.login_element.when_present(60)
   self.username = username
   self.password = password
   self.signIn_element.click
end

页面的 HTML 代码:

<div class="user-display">
<span class="user-actions">
    <a rel="nofollow" href="/RedisCache/Users/Account/LogOn?ReturnUrl=%2FRedisCache%2F">
        Sign In
    </a>
</span>
</div>

通过黄瓜运行我的脚本我已经看到了下一个异常(PhantomJS):

timed out after 60 seconds, waiting for {:text=>"Sign In", :tag_name=>"a"} to become present(Watir::Wait::TimeoutError)

当我尝试使用 irb “手动”重现这种情况时,我在尝试单击登录链接(PhantomJS)时遇到了下一个异常:

Selenium::WebDriver::Error::ElementNotVisibleError: Error Message => 'Element is not currently visible and may not be manipulated'
caused by Request => {"headers":{"Accept":"application/json","Connection":"close","Content-Length":"2","Content-Type":"application/x-www-form-urlencoded","Host":"127.0.0.1:8910","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{}","postRaw":"{}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/ed325840-496d-11e4-9570-2d0549eccb44/element/%3Awdc%3A1412169736003/click"} (ReqHand)
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:638:in `raw_execute'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:616:in `execute'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:373:in `clickElement'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/common/element.rb:54:in `click'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:132:in `click'

当我在 IE 浏览器中运行脚本时,无论我是在运行黄瓜还是试图“手动”单击此链接,都会挂起。正如我所看到的,链接就像失去/接收焦点一样闪烁,仅此而已。 我试图降级 PhantomJS 版本并重新安装它,但没有任何帮助。 有什么建议么? IE版本为9.0.8112 PhantomJS 版本是 1.9.7.0 所有 Gem 都有最新版本。

【问题讨论】:

  • WW 并不是真正的跨浏览器解决方案。您可能应该记住这一点:watirmelon.com/2014/03/13/…
  • 正如 Alister 在他的文章中所说,“但是,作为顾问,我经常听到经理们说我们必须在 Internet Explorer 中运行我们的 WebDriver 自动化测试。” - 这也是我的问题 :) 但是经过几次艰难的对话,我们最终决定在 IE 中只运行小场景。从技术角度来看:正如约翰逊建议的那样,我已经更改了 element 的选择器并使其正常工作。所以我接受他的回答。

标签: cucumber phantomjs watir-webdriver


【解决方案1】:

我过去曾遇到过 :text 选择器和 IEDriver 的问题。这对我来说非常挑剔,有时通过,有时失败。我会切换到其中之一:

link(:SignInLink, href: "/RedisCache/Users/Account/LogOn?ReturnUrl=%2FRedisCache%2F")

link(:SignInLink, xpath: "//span[@class='user-actions']/a[contains(@href,'Users/Account/LogOn')]")

【讨论】:

  • 解决了 phantomjs 的问题:这是我们主页调整大小的问题(因为 phantomjs 窗口的起始大小 Sign In lnk 不在视野范围内)。解决方案:在开始时将 phantomjs 窗口的大小调整为适当的值。至于第二个问题(使用 IE 浏览器),我仍在寻找解决方案。更改选择器并没有解决问题。不过谢谢你的提议。
猜你喜欢
  • 1970-01-01
  • 2018-08-25
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2013-10-08
  • 1970-01-01
  • 2016-09-07
相关资源
最近更新 更多