The*_*kie 1 ruby clipboard automation selenium-webdriver
我可以成功地将文本复制到剪贴板,并且还在指定的路径上创建了新文件,但是粘贴的数据是错误的.正在粘贴此数据(文件: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
Run Code Online (Sandbox Code Playgroud)
我的代码
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
Run Code Online (Sandbox Code Playgroud)
由于win32-clipboard发出错误所以我使用了剪贴板gem.
上面的代码与irb工作正常,但我无法在我的脚本中执行相同的操作.
在史蒂夫的帮助和一些修改下,这就是现在的工作代码
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
Run Code Online (Sandbox Code Playgroud)
正如您将在上面的代码中看到的那样,driver.find_element(:xpath,'html/body/div[31]/div[1]/button').click关闭文本所在的窗口.当我在粘贴剪贴板数据之前关闭它时,我得到了错误的值.在关闭窗口后,webdriver句柄将清除剪贴板数据.
现在这段代码完美无缺.