4

In StackOverflow, there is already a similar post about this topic. Though the OP of the post answered stating that "this is a bug in Windows" and that "I did not find a solution, I just cut part of my code out".

I have a slightly different circumstance. I cannot cut the part of the code out but I can use different web browsers.

My Circumstance:

I need to write a specific text into a text box. Unfortunately, the text I am writing includes few instances of emojis, thus send_keys() was not an option. Instead, with the help of StackOverflow, I end up using pyperclip.copy(text) and element.send_keys(Keys.CONTROL,'v') which works fine (it successfully copied the emojis).

But I also need the ChromeDriver to be --headless. Everything worked well, except for the copy-paste part.

My question

My question is: how should I tackle this issue?

  • Web browser must be hidden (ex. --headless at ChromeDriver)
  • it can write emojis into a text element
  • has to work on Windows OS (Windows 7 - 10)
Programer Beginner
  • 1,377
  • 6
  • 21
  • 47
  • 2
    The clipboard is a service/daemon provided by the window manager, not a feature in Chrome. So if you are running headless, there might not be a system clipboard available. – Håken Lid Oct 31 '18 at 16:14
  • @HåkenLid Hmm, do you know of any alternative solution to my issue? (writing emojis into a textbox element, while having the web browser 'hidden')? – Programer Beginner Oct 31 '18 at 16:22
  • Is using a different browser driver an option? I think the Firefox selenium driver should support characters beyond the BMP in `send_keys`. – Håken Lid Oct 31 '18 at 16:22
  • @HåkenLid It is an option. I will try it then. – Programer Beginner Oct 31 '18 at 16:23
  • @HåkenLid the firefox selenium driver (`geckodriver`) did not work. Not only it was not able to send the emoji via send_keys but my code also got this [issue #1184](https://github.com/mozilla/geckodriver/issues/1184). – Programer Beginner Oct 31 '18 at 16:55
  • 1
    @HåkenLid ok, after further testing I can confirm that *firefox selenium driver does support emoji send_keys*. However, I still cannot confirm that it works on headless mode and mainly, my code got into multiple issue (since it was coded with `ChromeDriver` in mind. So I am still open to other suggestions. – Programer Beginner Oct 31 '18 at 17:27
  • There's an open issue about this in chromium, so it might get fixed in the future. https://bugs.chromium.org/p/chromedriver/issues/detail?id=2269 – Håken Lid Oct 31 '18 at 17:47
  • @HåkenLid so after long process of refactoring the code to make it compatible with Firefox web driver, I manage to successfully run my program. But even though it 'solved' my issue, there are a lot of limitation to using firefox driver so if you want, you can put it as an answer but I will accept it if nobody else can think of another alternative solution :) – Programer Beginner Oct 31 '18 at 18:11
  • @ProgramerBeginner Does `element.send_keys(Keys.CONTROL,'v')` works in Firefox headless mode? – user3595632 Nov 18 '18 at 06:43

1 Answers1

-1

I had the same issue so I used klembord instead of pyperclip.

https://pypi.org/project/klembord/

# pip install webdriver-manager
# pip install klembord
# pip install selenium
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
import klembord
klembord.init()
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options,executable_path=GeckoDriverManager().install())
print("Headless Firefox Initialized. Wait for output")
driver.get("https://www.lipsum.com")
l = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div[3]/div[1]/p")
klembord.set_text(l.text) # setting text to clipboard
print("Check clipboard by pressing WIN + V or CTRL +V")
driver.quit()