How to paste from pyperclip in Selenium headful driver running on pyvirtualdisplay

108 views Asked by At

I am trying to copy a string of text (contains emojis) using pyperclip and paste it using Selenium's send_keys(Keys.CONTROL + "v"), it doesn't work.

Ps. I understand that running on headless won't work because there is no GUI hence I'm trying with pyvirtualdisplay + pyperclip (I verified pyperclip worked)

What I have tried:

  1. Using geckodriver which doesn't work for my case because it doesn't support all the emoji I'm using
  2. Run javascript code, also doesn't work because its not an input type (its Whatsapp)
  3. Manually instruct Selenium to select all the emojis which works but is agonizingly slow and it crashes from time to time (I'm running it on cron)

So I guess my question is:

  1. Is there anyway I can paste my copied text from pyperclip in this setup?
  2. Any pointers I can use to investigate why my script crashes occasionally?

Sample Code and crash report is appended as below:

Sample Code:

from pyvirtualdisplay import Display

display = Display(visible=False, size=(1280, 1024))
display.start() # *Start display before initializing pyperclip*

import pyperclip
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=1920,1080")
options.add_argument("--user-agent==Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
options.add_argument("--user-data-dir={}".format(self.chrome_cache))

.
.
.

pyperclip.copy("️️")
#print(pyperclip.paste()) # *This works and is commented when running the script*
elem.send_keys(Keys.CONTROL + "v")

Crash Report:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /snap/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
0

There are 0 answers