Watir::Exception::UnknownObjectException: 无法定位元素
Watir::Exception::UnknownObjectException: unable to locate element
This is the drop-down from where I will select the various contract type
This the DIV, UL and LI classes which I have used in the code
irb(main):128:0> li_count_in_ul9 = browser.div(:class => "select2-drop select2-display-none select2-with-searchbox select2-drop-active").ul(:class => "select2-results").lis(:class => "select2-results-dept-0 select2-result select2-result-selectable")
=> #<Watir::LICollection:0x000000028ac0d0 @parent=#<Watir::UList:0xfc812aa2 located=false selector={:class=>"select2-results", :tag_name=>"ul"}>, @selector={:class=>"select2-results-dept-0 select2-result select2-result-selectable", :tag_name=>"li"}>
irb(main):129:0> li_count_in_ul9[0].click
Watir::Exception::UnknownObjectException: unable to locate element, using {:class=>"select2-results-dept-0 select2-result select2-result-selectable", :tag_name=>"li", :index=>0}
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:508:in `assert_exists'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:114:in `click'
from (irb):129
from C:/Ruby23-x64/bin/irb.cmd:19:in `<main>'
irb(main):130:0> sleep 5
=> 5
irb(main):131:0>
问题似乎出在:
browser.div(:class => "select2-drop select2-display-none select2-with-searchbox select2-drop-active")
从 HTML 的图像来看,那不是包含 li
元素的 div
。它是包含元素的同级 div
。由于它有一个 ID,您可以通过以下方式找到它:
browser.div(:id => "select2-drop")
那么最终结果是:
li_count_in_ul9 = browser.div(:id => "select2-drop").ul(:class => "select2-results").lis(:class => "select2-results-dept-0 select2-result select2-result-selectable")
来自 HTML 共享,这可能被过度指定了。您可以简单地做到:
li_count_in_ul9 = browser.div(:id => "select2-drop").lis(:class => "select2-result-selectable")
This is the drop-down from where I will select the various contract type
This the DIV, UL and LI classes which I have used in the code
irb(main):128:0> li_count_in_ul9 = browser.div(:class => "select2-drop select2-display-none select2-with-searchbox select2-drop-active").ul(:class => "select2-results").lis(:class => "select2-results-dept-0 select2-result select2-result-selectable")
=> #<Watir::LICollection:0x000000028ac0d0 @parent=#<Watir::UList:0xfc812aa2 located=false selector={:class=>"select2-results", :tag_name=>"ul"}>, @selector={:class=>"select2-results-dept-0 select2-result select2-result-selectable", :tag_name=>"li"}>
irb(main):129:0> li_count_in_ul9[0].click
Watir::Exception::UnknownObjectException: unable to locate element, using {:class=>"select2-results-dept-0 select2-result select2-result-selectable", :tag_name=>"li", :index=>0}
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:508:in `assert_exists'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/elements/element.rb:114:in `click'
from (irb):129
from C:/Ruby23-x64/bin/irb.cmd:19:in `<main>'
irb(main):130:0> sleep 5
=> 5
irb(main):131:0>
问题似乎出在:
browser.div(:class => "select2-drop select2-display-none select2-with-searchbox select2-drop-active")
从 HTML 的图像来看,那不是包含 li
元素的 div
。它是包含元素的同级 div
。由于它有一个 ID,您可以通过以下方式找到它:
browser.div(:id => "select2-drop")
那么最终结果是:
li_count_in_ul9 = browser.div(:id => "select2-drop").ul(:class => "select2-results").lis(:class => "select2-results-dept-0 select2-result select2-result-selectable")
来自 HTML 共享,这可能被过度指定了。您可以简单地做到:
li_count_in_ul9 = browser.div(:id => "select2-drop").lis(:class => "select2-result-selectable")