Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

408
Views
How to adapt a Selenium Javascript "sendKeysWithEmojis" code to Python

I just found a possible solution to a sendKeysWithEmojis issue I am having and it is written for Selenium in Javascript and I would like to translate it to python so I can test in in my infrastructure and be able to . The solution is the following: https://stackoverflow.com/a/60478726/12474157

 async sendKeysWithEmojis(element, text) {
        const script = `var elm = arguments[0],
        txt = arguments[1];elm.value += txt;
        elm.dispatchEvent(new Event('keydown', {bubbles: true}));
        elm.dispatchEvent(new Event('keypress', {bubbles: true}));
        elm.dispatchEvent(new Event('input', {bubbles: true}));
        elm.dispatchEvent(new Event('keyup', {bubbles: true}));`;
        await this.driver.executeScript(script, element, text);
    }

To execute:

const element = await this.driver.findElement(selector);
await sendKeysWithEmojis(element, '๐Ÿš€๐Ÿ˜€ This one shall pass ๐Ÿš€๐Ÿ˜€');
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

Frankly, I don't understand what is the problem.

I took first answer from your link and it works for me with Chrome and with text '๐Ÿš€๐Ÿ˜€ This one shall pass ๐Ÿš€๐Ÿ˜€' on page Google.com

from selenium import webdriver

JS_ADD_TEXT_TO_INPUT = """
  var elm = arguments[0], txt = arguments[1];
  elm.value += txt;
  elm.dispatchEvent(new Event('change'));
  """

browser = webdriver.Chrome()
browser.get("https://google.com/")
elem = browser.find_element_by_name('q')

text = '๐Ÿš€๐Ÿ˜€ This one shall pass ๐Ÿš€๐Ÿ˜€'

browser.execute_script(JS_ADD_TEXT_TO_INPUT, elem, text)

But if you really need version from JavaScript then you have to only change text in JS_ADD_TEXT_TO_INPUT

from selenium import webdriver

JS_ADD_TEXT_TO_INPUT = """
        var elm = arguments[0],
        txt = arguments[1];elm.value += txt;
        elm.dispatchEvent(new Event('keydown', {bubbles: true}));
        elm.dispatchEvent(new Event('keypress', {bubbles: true}));
        elm.dispatchEvent(new Event('input', {bubbles: true}));
        elm.dispatchEvent(new Event('keyup', {bubbles: true}));
"""

browser = webdriver.Chrome()
browser.get("https://google.com/")
elem = browser.find_element_by_name('q')

text = '๐Ÿš€๐Ÿ˜€ This one shall pass ๐Ÿš€๐Ÿ˜€'

browser.execute_script(JS_ADD_TEXT_TO_INPUT, elem, text)


EDIT:

Or maybe your problem is to put it in function

from selenium import webdriver

def sendKeysWithEmojis(element, text):
    JS_ADD_TEXT_TO_INPUT = """
            var elm = arguments[0],
            txt = arguments[1];elm.value += txt;
            elm.dispatchEvent(new Event('keydown', {bubbles: true}));
            elm.dispatchEvent(new Event('keypress', {bubbles: true}));
            elm.dispatchEvent(new Event('input', {bubbles: true}));
            elm.dispatchEvent(new Event('keyup', {bubbles: true}));
    """

    browser.execute_script(JS_ADD_TEXT_TO_INPUT, elem, text)

# --- main ---

browser = webdriver.Chrome()
browser.get("https://google.com/")

elem = browser.find_element_by_name('q')

text = '๐Ÿš€๐Ÿ˜€ This one shall pass ๐Ÿš€๐Ÿ˜€'

sendKeysWithEmojis(elem, text)

OR maybe problem is only on Windows - I tested on Linux.

OR maybe problem is Chrome/ChromeDriver - I tested on Chorme/ChromeDriver 95.0.4638.69

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!