pythonpython-3.xseleniumparsingselenium-webdriver

Selenium webdriver: unnecessary and repetitive values when sending keys


I am using Selenium webdriver to write text with some special features in the webdriver using the following code:

def typing_(self, text):
    time.sleep(1)
    new_text = []
    text_ = text.split(' ')
    
    for word in text_:
        new_text.append(word + ' ')
        if word.startswith('@') or word.startswith('&'):
            new_text.append(self.enter_key)
    # new_text is the result of parsing text and adding the function

    time.sleep(1)
    actions = ActionChains(self.driver)
    for word_ in new_text:
        
        if not isinstance(word_, str):
            word_()
            time.sleep(1)
        else:
            for char in word_:
                actions.send_keys(char[0])
                actions.perform()
                time.sleep(1)

Basically what it does is that it inputs some text and writes it,

I have also added a special characteristic: When characters "@" or "&" are present it counts the next characters until it finds a space " ", and just before it, presses enter key.

In this case I configured a function press_enter() so this part will be easy.

Example:

I think @guacamole(press enter here) is good for health

When I send the text to the webdriver, the result I am getting is the following: NOTE: It actually presses enter key in the right place

II I tI thI thiI thinI thinkI think I think @I think @gI think @guI think @guaI think @guacI think @guacaI think @guacamI think @guacamoI think @guacamolI think @guacamoleI think @guacamole 
I think @guacamole iI think @guacamole isI think @guacamole is I think @guacamole is gI think @guacamole is goI think @guacamole is gooI think @guacamole is goodI think @guacamole is good I think @guacamole is good fI think @guacamole is good foI think @guacamole is good forI think @guacamole is good for I think @guacamole is good for hI think @guacamole is good for heI think @guacamole is good for heaI think @guacamole is good for healI think @guacamole is good for healtI think @guacamole is good for healthI think @guacamole is good for health

Solution

  • def typing_(self, text):
        time.sleep(1)
        new_text = []
        text_ = text.split(' ')
        
        for word in text_:
            new_text.append(word + ' ')
            if word.startswith('@') or word.startswith('&'):
                new_text.append(self.enter_key)
        # new_text is the result of parsing text and adding the function
    
        time.sleep(1)
        actions = ActionChains(self.driver)
        for word_ in new_text:
            
            if not isinstance(word_, str):
                word_()
                time.sleep(1)
            else:
                for char in word_:
                    actions.send_keys(char[0])
                    actions.perform()
                    actions.reset_actions()
                    time.sleep(1)
    

    Try adding reset_actions in forloop else the action will chain the previous send key action

    If that doesn't work then use any of two solution:

    first move action initialization to inside of loop:

    for char in word:
        actions = webdriver.ActionChains(driver)
        actions.send_keys(char[0])
        actions.perform()
        time.sleep(1)
    

    ** updated seelnium to alpha v4**

    pip install selenium==4.0.0.a7
    

    for second solution your code will work as it is

    Just more information:

    https://github.com/SeleniumHQ/selenium/issues/6837

    reset_actions() is not reseting the actions.

    There was a bug for this and it was fixed in latest seleniumv4

    in selenium v3 you can use the above mention fix or

    for char in word:
        actions.send_keys(char[0])
        time.sleep(1)
        
        actions.perform()
        actions.w3c_actions.clear_actions()
        for device in actions.w3c_actions.devices:
          device.clear_actions()