C# Selenium send an emoji using SendKeys
Asked Answered
K

4

0

I am trying to sendkeys an emoji. I have tried to send it by copying the emoji πŸ‘, but it raised this exception:

OpenQA.Selenium.WebDriverException: unknown error: ChromeDriver only supports characters in the BMP

I tried to send it as unicode but without any success. It isn't the intended sign.

input.SendKeys("/u1F44D")

What is the proper way to send an emoji?

Searched and found this but it does not have an answer therefore I'm asking again.

Kobi answered 14/12, 2018 at 21:59 Comment(5)
Backslash instead of a regular slash? – Braunschweig
Have you tried just sending the ASCII characters that turn into the emoji? Not sure what these are ... maybe (y)? – Jens
Thanks for your input. @Braunschweig it doesn't work, instead I get some sort of unrecognized text - "ὄE". – Kobi
Thanks for your input. @Jens that is the ASCII equivalent of the emoji. – Kobi
So when you send it, does it work? – Jens
T
2

The issue is that you’re using ChromeDriver. As the exception message states, ChromeDriver only supports code points in the Unicode Basic Multilingual Plane at present. If you were to use another driver, say, FirefoxDriver or InternetExplorerDriver, sending the emoji would have better results. There are specific tests in the Web Platform Test suite that specifically send emojis, and these work for other browsers.

Incidentally, the correct way to send the character in C# would be

input.SendKeys("\u1F44D");
Tautonym answered 15/12, 2018 at 15:39 Comment(0)
M
2

You can achieve this in C# with Selenium using the IJavaScriptExecutor

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace ResearchDevelopment
{
    internal class Program
    {  
        static void Main(string[] args)
        {
            var driver = new ChromeDriver() { Url = "http://www.google.co.za" };

            var searchInput = driver.FindElement(By.XPath("//input[@aria-label='Search']"));

            PopulateElementJs(driver, searchInput, "πŸ‘Œ");

            searchInput.SendKeys(Keys.Enter); // Optional

            driver?.Quit();
        }

        public static void PopulateElementJs(IWebDriver driver, IWebElement element, string text)
        {
            var script = "arguments[0].value=' " + text + " ';";
            ((IJavaScriptExecutor)driver).ExecuteScript(script, element);
        }
    }
}

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Selenium.Chrome.WebDriver" version="76.0.0" targetFramework="net472" />
  <package id="Selenium.Support" version="3.0.0" targetFramework="net472" />
  <package id="Selenium.WebDriver" version="3.0.0" targetFramework="net472" />
</packages>
Maverick answered 19/9, 2019 at 13:35 Comment(1)
Thanks very much for that simple solution! My hero! – Hit
E
1

Hello Vibhav Ramcharan

I did a first test, on the Google search box it works! I did a second test to write on the modal window of a facebook group, but nothing happens!

From your example I have only replaced:

var searchInput = driver.FindElement(By.XPath("//input[@aria-label='Search']"));

with:

var searchInput = driver.FindElement(By.XPath("//div[@class='rq0escxv datstx6m k4urcfbm a8c37x1j']/div/div/div/div/div/div/span"));

The final is where we go to write the Post

If I use plain text without emoji, the method below works.

driver.FindElement(By.XPath("//div[@class='rq0escxv datstx6m k4urcfbm a8c37x1j']/div/div/div/div/div/div/span")).SendKeys("...My text of the post...");

Maybe I need to change this?

var script = "arguments[0].value='" + text + "';";
Evangelistic answered 20/10, 2020 at 15:1 Comment(0)
G
0

ive have found a bit complex but working solution for this problem

so

first you can create helper for clipboard or use System.Windows.Forms for simpler usage. There is helper class if someone would need this soultion

internal class ClipboardHelper
{
    [DllImport("user32.dll")]
    public static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll")]
    public static extern bool CloseClipboard();

    [DllImport("user32.dll")]
    public static extern bool EmptyClipboard();

    [DllImport("user32.dll")]
    public static extern IntPtr SetClipboardData(uint uFormat, IntPtr data);

    public static void SetText(string text)
    {
        if (OpenClipboard(IntPtr.Zero))
        {
            EmptyClipboard();
            SetClipboardData(13, Marshal.StringToHGlobalUni(text));
            CloseClipboard();
        }
    }
}

using forms library by SetClipboardText(string text)

and then using class from selenium named Actions you can perform pasting

new Actions(driver)
    .KeyDown(cmdCtrl)
    .SendKeys("v")
    .KeyUp(cmdCtrl)
    .Perform();

however you have first click element in what you gonna paste text so element have focus

Hope its gonna help someone :) if there is need for a bit different solution

Greenhorn answered 22/4 at 0:57 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.