简体   繁体   English

Capybara错误:Selenium :: WebDriver :: Error :: ElementNotVisibleError:元素不可见

[英]Capybara error: Selenium::WebDriver::Error::ElementNotVisibleError: element not visible

I am using the Trix WYSIWYG editor in my app. 我在我的应用程序中使用Trix WYSIWYG编辑器 For my capybara test: I want to fill in the editor. 对于我的水豚测试:我想填写编辑器。

I found the article: How to test basecamp's trip editor... which seemed promising. 我找到了这篇文章: 如何测试basecamp的旅行编辑器......这似乎很有希望。 Unfortunately it keeps giving me this error: 不幸的是它一直给我这个错误:

Selenium::WebDriver::Error::ElementNotVisibleError: element not visible Selenium :: WebDriver :: Error :: ElementNotVisibleError:元素不可见

So it appears that Capybara is finding the element ok, but it is just not interacting with it because Capybara must have some default setting to not interact with hidden/invisible elements. 所以似乎Capybara找到了元素ok,但它只是没有与它交互,因为Capybara必须有一些默认设置,不与隐藏/不可见元素交互。

After looking around I came upon this Stackoverflow question: Is it possible to interact with hidden elements with capybara . 环顾四周之后,我遇到了这个Stackoverflow问题: 是否可以与隐藏元素进行交互

From that post: I have already tried this: 从那篇文章:我已经尝试过这个:

def fill_in_trix_editor(id, value)
  Capybara.ignore_hidden_elements = false
  find(:xpath, "//*[@id='#{id}']").set(value)
  Capybara.ignore_hidden_elements = true
end

As well as this: 除此之外:

def fill_in_trix_editor(id, value)
  find(:xpath, "//*[@id='#{id}']", visible: false).set(value)
end

Any idea as to how I can get Capybara to fill in the editor? 关于如何让Capybara填写编辑器的任何想法? For what it is worth: I am using rails 5.1.1 and chromedriver=2.29.461585 值得一提的是:我使用的是rails 5.1.1chromedriver=2.29.461585

Short answer: You can't using selenium 简答:你不能使用硒

Longer answer: That error is selenium preventing you from interacting with a non-visible element because there would be no way for a user to click or send keys to a non-visible element. 更长的答案:该错误是selenium阻止您与不可见元素交互,因为用户无法单击或发送键到不可见元素。

If you really want to change the value of a hidden element, the only way would be using JS via execute_script but that most likely won't generate the events trix is expecting/using. 如果你真的想要改变隐藏元素的值,唯一的方法是通过execute_script使用JS,但很可能不会生成trix期望/使用的事件。 A much better solution would to be figure out what visible elements a user would interact with and interact with them directly. 一个更好的解决方案是弄清楚用户将与哪些可见元素进行交互并直接与它们进行交互。 Capybara with selenium does support calling set on visible contenteditable elements which is what trix appears to be using (along with custom elements) so something like 带有selenium的Capybara确实支持在可见的contenteditable元素上调用set ,这是trix似乎正在使用的(以及自定义元素),所以类似于

find(:css, 'trix-editor').set("New text") 

will probably work for you 可能会为你工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM