Justin Chee
Justin Chee

Reputation: 475

How can I get selenium to get a keyboard press of shift + enter at the same time using python?

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.

Upvotes: 2

Views: 3817

Answers (3)

Yingxu He
Yingxu He

Reputation: 150

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

Upvotes: 2

Booboo
Booboo

Reputation: 44313

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.

Upvotes: 3

Sodium
Sodium

Reputation: 1066

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();

Upvotes: 2

Related Questions