2014-09-30 85 views
0

有人可以幫我用紅寶石硒問題。我剛開始學習ruby,我不知道這個代碼不工作。我可以很容易地在某些網頁上按名稱或類或id查找某些元素,但在某些情況下,日誌只會引發錯誤。我不知道。Ruby Selenium:元素目前不可見錯誤

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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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 
+0

更好地定位元素。嘗試使用瀏覽器 - >檢查元素 - >複製獨特的css定位器。 另外閱讀有關使用定位器或觀看youtube教程: https://www.youtube.com/watch?v=BnsylMLBcoY&list=PLwbuZz2PgXBrGx2kjegs-C4iOtoxXmxM2&index=43 – SkorpEN 2014-09-30 12:00:23

回答

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() 
相關問題