Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么传入css选择器时会出现无效属性错误?_Ruby_Watir_Watir Webdriver - Fatal编程技术网

Ruby 为什么传入css选择器时会出现无效属性错误?

Ruby 为什么传入css选择器时会出现无效属性错误?,ruby,watir,watir-webdriver,Ruby,Watir,Watir Webdriver,堆栈溢出 这就是我要做的 def get_element_from_list(root, item, index) @browser.elements(:css => root).each do |element| if element.present? return element.element(:css => item, :index => index) end end raise Selenium::WebDriver::Err

堆栈溢出

这就是我要做的

def get_element_from_list(root, item, index)
  @browser.elements(:css => root).each do |element|
    if element.present?
      return element.element(:css => item, :index => index)
    end
  end

  raise Selenium::WebDriver::Error::NoSuchElementError
end

get_element_from_list('div[class*=x-combo-list]', 'x-combo-list-item', index).click
告诉我Watir::Exception::MissingWayOfFindingObject异常:无效属性::css

我不明白的是如果我只是这么做

@browser.elements(:css => 'div[class*=x-combo-list]').each do |element|
  if element.present?
    return element.element(:css => 'x-combo-list-item', :index => index)
  end
end

基本上是用实际的字符串替换根和项,它可以正常工作。

我认为可能存在一个错误,阻止使用:css和:index locator-定位元素

您可以通过获取元素集合,然后获取特定索引处的元素来解决此问题:

return element.elements(:css => 'x-combo-list-item')[index]
(注意,我认为这个css选择器可能是错误的。它可能是
.x-combo-list-item

或者,假设
x-combo-list-item
实际上是元素的类,您可以执行以下操作:

return element.element(:class => 'x-combo-list-item', :index => index)

您是否检查过根和项在调用此函数的任何函数中都不是nil?是的,绝对不是nil。您真的确定直接插入字符串的第二位代码可以工作吗?似乎有点奇怪,会有一个“x-combo-list-item”标记(而不是类)。每当我尝试组合:css和:index定位器时,我似乎都会遇到这种异常,但我无法从代码中找出原因。你是对的,它不是一个标记,而是一个类。但是,将其更改为div[class=x-combo-list-item]并不会更改错误。好的,您的索引解决方案解决了这个问题。您是对的,它是一个类,但我试图将此方法推广到我们遇到的不同类型的列表,从而使用css。