Selenium 水豚:page.execute_script()不工作

Selenium 水豚:page.execute_script()不工作,selenium,drag-and-drop,capybara,rspec-rails,simulate,Selenium,Drag And Drop,Capybara,Rspec Rails,Simulate,我正在尝试使用rspec功能中的jquery.simulate库模拟拖放。规范中的execute\u脚本行为: page.execute_script("$('#slide_1').draggable();") page.evaluate_script("$('#slide_1').simulate('drag', {dragTarget: '#library_swap', interpolation: {stepWidth: 10, stepDelay: 300 }});") page.

我正在尝试使用rspec功能中的jquery.simulate库模拟拖放。规范中的execute\u脚本行为:

 page.execute_script("$('#slide_1').draggable();")
 page.evaluate_script("$('#slide_1').simulate('drag', {dragTarget: '#library_swap', interpolation: {stepWidth: 10, stepDelay: 300 }});")
 page.evaluate_script("$('#slide_1').simulate('drop');")

如果我在chrome控制台上运行execute脚本内部的行,它可以正常工作(拖放模拟可以工作),但不能使用execute_脚本,因为您没有收到任何错误,所以您传递给execute_脚本的JS代码实际上正在执行。由于您没有看到预期的行为,最有可能的解释是,您在元素实际出现在页面上之前执行JS,而该元素将只是默默地不做任何事情。有一件事让我对代码感到困惑,那就是为什么你要在
#slide_1
元素上调用
draggable
,因为我假设你的应用程序中已经调用了该元素。无论如何-在
execute\u脚本调用之前添加期望值,以确保元素实际位于页面上

expect(page).to have_css('#slide_1')
execute_script("$('#slide_1')...
slider = page.find(:css, '#slide_1')
execute_script("$(arguments[0]). ...    ", slider)
另外请注意,没有任何理由需要为此使用三个不同的
execute\u script
调用,您可以将它们组合成一个。在最新版本的Capybara中,您还可以通过不再指定选择器,而是将元素传递给execute_脚本来消除命令

slider = page.find(:css, '#slide_1')
execute_script("$(arguments[0]). ...    ", slider)

您是否有任何错误,或者只是没有达到您的预期?您对第二和第三条语句使用
evaluate\u script
有什么原因吗?@ThomasWalpole没有,我没有收到任何错误,但脚本没有执行。在使用evaluate脚本之前,我使用了execute_脚本,但这两个脚本都不起作用。谢谢Thomas。我通过在第二行之后添加一个sleep来解决这个问题。实际上,脚本正在执行,但似乎无法在UI上工作。