The Rookie
The Rookie

Reputation: 605

How to paste clipboard contents to a file?

I can successfully copy the text to the clipboard and new file is also being created at the specified path but data being pasted is wrong.This data is being pasted (File:0x1ff09c8)

I also tried using 'win32/clipboard' but was getting a error 'can't load win32/clipboard'.

As i am using jruby so i installed 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

My code

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

As win32-clipboard was giving a error so i used clipboard gem.

Above code is working fine with irb but i am not able to do the same in my script.

Upvotes: 2

Views: 1510

Answers (1)

The Rookie
The Rookie

Reputation: 605

With help from steve and some modification this is the working code now

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

As you will see in above code driver.find_element(:xpath,'html/body/div[31]/div[1]/button').click is to close the window where the text is present. And as i was closing it before i could paste clipboard data i was getting wrong values.The webdriver handle would clear the clipboard data after it would close the window.

Now this code works perfectly fine.

Upvotes: 5

Related Questions