我可以成功地将文本复制到剪贴板,并且也在指定路径创建新文件,但粘贴的数据是错误的。正在粘贴此数据(文件:0x1ff09c8)
我还尝试使用“win32/clipboard”,但收到错误“无法加载 win32/clipboard”。
由于我使用的是 jruby,所以我安装了 gem win32-clipboard
$ jruby -S gem install win32-clipboard
Building native extensions. This could take a while...
ERROR: Error installing win32-clipboard:
ERROR: Failed to build gem native extension.
c:/jruby-1.7.4/bin/jruby.exe extconf.rb
NotImplementedError: C extension support is not enabled. Pass -Xcext.enabled=tru
e to JRuby or set JRUBY_OPTS or modify .jrubyrc to enable.
(root) at c:/jruby-1.7.4/lib/ruby/shared/mkmf.rb:8
require at org/jruby/RubyKernel.java:1054
(root) at c:/jruby-1.7.4/lib/ruby/shared/rubygems/custom_require.rb:1
(root) at extconf.rb:7
Gem files will remain installed in c:/jruby-1.7.4/lib/ruby/gems/shared/gems/win3
2-api-1.4.8 for inspection.
Results logged to c:/jruby-1.7.4/lib/ruby/gems/shared/gems/win32-api-1.4.8/ext/g
em_make.out
我的代码
require 'clipboard'
WAIT.until { driver.find_element(:id, 'btnShowEmbedCode') }.click
sleep 3
em = WAIT.until { driver.find_element(:xpath, ".//*[@id='clipboardtext']") }
em.text
driver.find_element(:xpath, 'html/body/div[31]/div[1]/button').click
File.open('copy_embed_code.html', 'w') do |f|
f.truncate(0)
f << Clipboard.("#{f}")
end
由于 win32-clipboard 出现错误,所以我使用了剪贴板 gem。
上面的代码在 irb 中运行良好,但我无法在我的脚本中执行相同的操作。
最佳答案
在 steve 的帮助下并进行一些修改,这就是现在的工作代码
e = WAIT.until { driver.find_element(:xpath, ".//*[@id='clipboardtext']") }
e.text
File.open('copy_embed_code.html', 'w') do |f|
f.truncate(0)
f << e.text
end
driver.find_element(:xpath, 'html/body/div[31]/div[1]/button').click
end
正如您在上面的代码中看到的 driver.find_element(:xpath,'html/body/div[31]/div[1]/button').click
是关闭窗口文本已存在。当我在粘贴剪贴板数据之前关闭它时,我得到了错误的值。网络驱动程序句柄将在关闭窗口后清除剪贴板数据。
现在这段代码工作得很好。
关于ruby - 如何将剪贴板内容粘贴到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17593843/