Ruby Selenium:元素当前不可见错误

时间:2014-09-30 11:12:53

标签: ruby selenium

有人可以帮我解决ruby Selenium的问题。我刚刚开始学习ruby,我不知道这个代码是不行的。我可以在某些网页上轻松地按名称或类或ID找到一些元素,但在某些情况下,日志只会抛出错误。我不知道。

P.s。:请提供一些关于红宝石硒的神教程和文档。

THX

HTML结构

             
<div class="row" style="text-align: center;">

    <label class="field" for="uname" style="display: inline-block; width: 25%;"></label>
    <input id="uname" type="text" name="username" value=""></input>

</div>
<div class="row" style="text-align: center;">

    <label class="field" for="passwd" style="display: inline-block; width: 25%;"></label>
    <input id="passwd" type="password" name="password" value=""></input>

</div>

Ruby代码

require "selenium-webdriver"
Selenium::WebDriver::Firefox::Binary.path='C:\Java\mozilla\firefox.exe'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://admin/login.jsp"

element = driver.find_element(:name, 'username')
element.send_keys "root"
element = driver.find_element(:name, 'password')
element.send_keys "root"
element.submit()

IRB日志

D:\dev\selenium>irb main.rb
DL is deprecated, please use Fiddle
main.rb(main):001:0> require "selenium-webdriver"
=> true
main.rb(main):002:0> Selenium::WebDriver::Firefox::Binary.path='C:\Java\mozilla\firefox.exe'
=> "C:\\Java\\mozilla\\firefox.exe"
main.rb(main):003:0>
main.rb(main):004:0* driver = Selenium::WebDriver.for :firefox
=> #<Selenium::WebDriver::Driver:0xc7bad50a browser=:firefox>
main.rb(main):005:0> driver.navigate.to "http://192.168.211.159/mw/admin/login.jsp"
=> ""
main.rb(main):006:0>
main.rb(main):007:0* element = driver.find_element(:name, 'username')
Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"name","selector":"username"}
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/driver-component.js:9618:133:in `FirefoxDriver.prototype.findElementInternal_'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/driver-component.js:9627:3:in `FirefoxDriver.prototype.findElement'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/command-processor.js:11612:16:in `DelayedCommand.prototype.executeInternal_/h'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/command-processor.js:11617:7:in `DelayedCommand.prototype.executeInternal_'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/command-processor.js:11559:5:in `DelayedCommand.prototype.execute/<'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:638:in `raw_execute'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:616:in `execute'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:584:in `find_element_by'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/common/search_context.rb:42:in `find_element'
        from main.rb:7
        from C:/Ruby200-x64/bin/irb:12:in `<main>'main.rb(main):008:0> element.send_keys "root"
NoMethodError: undefined method `send_keys' for nil:NilClass
        from main.rb:8
        from C:/Ruby200-x64/bin/irb:12:in `<main>'
main.rb(main):009:0> element = driver.find_element(:name, 'password')
Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"name","selector":"password"}
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/driver-component.js:9618:133:in `FirefoxDriver.prototype.findElementInternal_'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/driver-component.js:9627:3:in `FirefoxDriver.prototype.findElement'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/command-processor.js:11612:16:in `DelayedCommand.prototype.executeInternal_/h'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/command-processor.js:11617:7:in `DelayedCommand.prototype.executeInternal_'
        from [remote server] file:///C:/Users/dpecnik/AppData/Local/Temp/webdriver-profile20140930-6796-1dzou78/extensions/fxdriver@googlecode.com/components/command-processor.js:11559:5:in `DelayedCommand.prototype.execute/<'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:638:in `raw_execute'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:616:in `execute'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:584:in `find_element_by'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/common/search_context.rb:42:in `find_element'
        from main.rb:9
        from C:/Ruby200-x64/bin/irb:12:in `<main>'main.rb(main):010:0> element.send_keys "root"
NoMethodError: undefined method `send_keys' for nil:NilClass
        from main.rb:10
        from C:/Ruby200-x64/bin/irb:12:in `<main>'
main.rb(main):011:0> element.submit()main.rb(main):011:0>
NoMethodError: undefined method `submit' for nil:NilClass
        from main.rb:11

1 个答案:

答案 0 :(得分:0)

我发现了什么问题...... 网站由框架组成,您需要先切换到框架,然后才能继续。

require "selenium-webdriver"
Selenium::WebDriver::Firefox::Binary.path='C:\Java\mozilla\firefox.exe'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://admin/login.jsp"

driver.switch_to.frame "mainFrame" 

element = driver.find_element(:name, 'username')
element.send_keys "root"
element = driver.find_element(:name, 'password')
element.send_keys "root"
element.submit()