Ruby Selenium:元素当前不可见错误
谁能帮我解决ruby Selenium的问题吗。我刚开始学习ruby,我不知道这段代码有什么地方不起作用。我可以通过名称、类或id在一些网页上轻松地找到一些元素,但在某些情况下,日志只会抛出错误。我不知道是什么 附言:请提供一些关于ruby selenium的god教程和文档 Thx HTML结构 内部评级日志Ruby Selenium:元素当前不可见错误,ruby,selenium,Ruby,Selenium,谁能帮我解决ruby Selenium的问题吗。我刚开始学习ruby,我不知道这段代码有什么地方不起作用。我可以通过名称、类或id在一些网页上轻松地找到一些元素,但在某些情况下,日志只会抛出错误。我不知道是什么 附言:请提供一些关于ruby selenium的god教程和文档 Thx HTML结构 内部评级日志 D:\dev\selenium>irb main.rb DL is deprecated, please use Fiddle main.rb(main):001:0> re
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
我发现了问题所在。。。 网站是由框架组成的,你们需要先切换到框架,然后才能继续
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()
更好地定位元素。尝试使用浏览器->检查元素->复制唯一css定位器。另外,请阅读有关使用定位器的内容或观看youtube教程:
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
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()