Use Python \text{Python} Python to simulate a FireFox or Chrome \text{FireFox or Chrome} FireFox or Chrome browser, Send_keys {send\_keys} send_keys sends the data to the input \text{input} input text box. When the number of bytes is small, no exception is detected, but when sending long text, There will be the phenomenon of stalling or blocking, resulting in the real-time performance of data decreased a lot.

Send_keys \text{send\_keys} send_keys

def send_keys(self, *keys_to_send) :
    """ Sends keys to current focused element. :Args: - keys_to_send: The keys to send. Modifier keys constants can be found in the 'Keys' class. """
    typing = keys_to_typing(keys_to_send)
    if self._driver.w3c:
        for key in typing:
            self.key_down(key)
            self.key_up(key)
    else:
        self._actions.append(lambda: self._driver.execute(
            Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {'value': typing}))
    return self
    
def keys_to_typing(value) :
    """Processes the values that will be typed in the element."""
    typing = []
    for val in value:
        if isinstance(val, Keys):
            typing.append(val)
        elif isinstance(val, int):
            val = str(val)
            for i in range(len(val)):
                typing.append(val[i])
        else:
            for i in range(len(val)):
                typing.append(val[i])
    return typing
Copy the code

You can see that the keys_to_typing \text{keys\_to\_typing} keys_to_typing function breaks value \text{value} values into a list of characters one by one, and then does something else, It’s not entirely clear why he did this (without further exploration), but it is certain that when there is a large amount of data, it will greatly increase the time of send_keys \text{send\_keys}, resulting in a staid situation.

The solution is to embed js \text{js} js for assignment:

js = "element = document.getElementById('text');" \
	 "element.value = 'xxx';"
driver.execute_script(js)
Copy the code

This avoids the stuttering problem caused by split characters, But it also brings a new problem – unable to use selenimu.webdriver.com mon. The keys \ text {selenimu.webdriver.com mon. Keys} selenimu.webdriver.com mon. Keys To trigger the event. Because sending text above does not trigger any events (such as the change \text{change} change event), which are often used to end an input refresh, we need to find a trigger scheme.

FireEvents or dispatchEvent \text{fireEvents or dispatchEvent} fireEvents or dispatchEvent The former is available in older or lower level browsers, while the latter is available in FireFox or Chrome \text{FireFox or Chrome} FireFox or Chrome. Since I’m using FireFox \text{FireFox} FireFox, it looks like this:

js = "element = document.getElementById('text');" \
	 "element.value = 'xxx';" \
	 "event = document.createEvent('HTMLEvents');" \
	 "event.initEvent('change', true, true);" \
	 "element.dispatchEvent(event);"
driver.execute_script(js)
Copy the code

Perfect solution to the problem of sending long text caton, but if you need to send a lot of data, the js \ text {js} js code in some part of it is not necessary to perform multiple, but because it is embedded in the Python \ text} {Python Python, so it have to figure out a way… Or, I don’t know how to optimize, I haven’t learned js \text{js} and js is my first attempt to embed it in Python \text{Python} Python.

In addition, some users said that you can also copy the data to the paste board and paste directly into the input box, without testing.