Home > Mobile >  Remote driver and pasting option. Does it exist?
Remote driver and pasting option. Does it exist?

Time:01-26

So I am trying to copy and than paste something in a file. And it is working without a problem in non remote setting.

How can I paste with remote driver?

I click copy to clipboard button and than have following function for paste.

def copy_recipient_mnemonic_to_file():
   paste = pyperclip.paste()
   with open('/Users/dusandev/Desktop/StreamFlowWebTests/reporting/wallets/recipient_mnemonic.txt', 'w') as file:
       file.write(str(paste))

But its not working. In remote capability, it is not pasting text that is copied from clipboard. It is pasting something I copied in my os.

Thank you in advance for your help!

CodePudding user response:

Pypeclip will work only if you run the remote browser on the same machine with your tests. Otherwise it will invoke actions for the local clipboard while your browser launched on some other env.

Getting clipboard value from the remote browser

Unfortunately, Selenium Grid has no clipboard support.

For getting copied text I suggest just paste it to some textarea and get the value attribute.

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys


# open some 3rd-party page with textarea 
driver.execute_script('''window.open("https://www.w3schools.com/bootstrap/bootstrap_forms_inputs.asp","_blank");''')
# note window_handles[-1] is the newest window for chrome, for other browsers it might be other index
driver.switch_to.window(driver.window_handles[-1])

# find textarea, clear it and paste from clipboard
textarea = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.TAG_NAME, 'textarea')))
textarea.clear()
# ctrl v (or use command v)
ActionChains(driver).key_down(Keys.CONTROL, textarea).send_keys('v').key_up(Keys.CONTROL, textarea).perform()

# getting pasted text
clipboard_value = textarea.get_attribute('value')

# switch back to original window
driver.close()
driver.switch_to.window(driver.window_handles[0])

print(clipboard_value )
  •  Tags:  
  • Related