2

I've made a whatsapp bot and a web scraper to get coronavirus cases and I want to send each data on a newline. Eg

Cases: x

Deaths: y

Recovered: z

But as im using whatsapp, /n doesn't work and would send each individual line. I've also tried ActionChains but that didn't work either. Any idea on how I can get a key combination for Shift + Enter? Thanks.

Justin Chee
  • 427
  • 1
  • 3
  • 14

3 Answers3

3

For Python:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).perform()

However, I could only get the above to work for Edge, not for Firefox nor Chrome.

Booboo
  • 38,656
  • 3
  • 37
  • 60
2

This is what you needed. Corresponding Java Implementation...

Actions actions = new Actions(driver);
// Press SHIFT + ENTER            
actions.keyDown(Keys.SHIFT)
        .sendKeys(Keys.RETURN)
        .build()
        .perform();
Sodium
  • 1,016
  • 1
  • 9
  • 22
  • And where did you import `Actions` from? `sendKeys` is *not* a Python method. Let me guess: You're a Java programmer. – Booboo May 16 '20 at 14:52
  • 1
    Oh ! sorry forget to mention, it's java implementation though. Thanks @Booboo – Sodium May 16 '20 at 14:54
  • I'd say this is still an acceptable answer since selenium with python and java are practically the same – YulkyTulky May 16 '20 at 15:48
0

BooBoo's answer is close but you still need to release the shift and enter key, or your input text might get very weird.

from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()

Refer to this answer

Yingxu He
  • 130
  • 1
  • 6