Home > Net >  Selenium Python ActionChain preform method
Selenium Python ActionChain preform method

Time:07-31

I need selenium and python to do the key combo Shift Enter, so that it inserts a new line without sending a message (\n sends the message), and with this code it doesn't produce an error, but it also doesn't create a new line. message_box variable is already defined and the messages there do show up.

Code trials:

action = ActionChains(driver)
message_box.send_keys("test1")
action.key_down(Keys.SHIFT)
action.send_keys(Keys.RETURN)
action.key_up(Keys.SHIFT)
message_box.send_keys("test2")

CodePudding user response:

You can chain the events in an ActionChains() and perform them in a chain using either of the following solutions:

  • Using SHIFT and RETURN:

    ActionChains(driver).move_to_element(message_box).send_keys("test1").key_down(Keys.SHIFT).send_keys(Keys.RETURN).key_up(Keys.SHIFT).send_keys("test2").perform()
    
  • Using SHIFT and ENTER:

    ActionChains(driver).move_to_element(message_box).send_keys("test1").key_down(Keys.SHIFT).send_keys(Keys.ENTER).key_up(Keys.SHIFT).send_keys("test2").perform()
    

Note : You have to add the following imports :

from selenium.webdriver.common.keys import Keys
  • Related