2013-07-11 197 views
1

我可以成功複製的文本複製到剪貼板,新文件也被在指定的路徑創建,但被粘貼數據wrong.This數據被粘貼(文件:0x1ff09c8)如何將剪貼板內容粘貼到文件中?

我也嘗試使用「的Win32 /剪貼板「,但得到一個錯誤」無法加載win32 /剪貼板「。

,因爲我使用JRuby,所以我安裝了寶石的Win32剪貼板

$ 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剪貼板是給一個錯誤,所以我使用的剪貼板上的寶石。

上面的代碼工作正常與IRB,但我不能在我的腳本中做同樣的事情。

+0

即使在irb中,我也不確定這將如何工作...首先你有一個文件'f','#{f}'是文件的對象ID,所以它應該看起來像File:0x1ff09c8這實際上是一個File對象ID。其次,Clipboard。(「some string」)不是剪貼板gem支持的格式...它期望Clipboard.copy或Clipboard.paste或Clipboard.clear ...我希望你的倒數第二行應該是'f < SteveTurczyn

+0

'File.open('copy_embed_code.html','w')do | f | f.truncate(0) f << Clipboard.paste end'相應地更新了我的代碼的最後一行。感謝史蒂夫的幫助。 :) –

回答

5

與史蒂夫的幫助,做一些修改,這是工作的代碼現在

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是關閉窗口,在文本存在。正如我在關閉它之前我可以粘貼剪貼板數據,我得到了錯誤的值。webdriver句柄會在關閉窗口後清除剪貼板數據。

現在這段代碼工作得很好。

相關問題