Python and Selenium - avoid submitting a form when send_keys () is on a newline

I am using Python 3 with selenium.

Suppose var = "whatever\nelse"

My problem is that when I use elem.send_keys(var)

it submits the form after "anyway" (due to newline)

How can I replace "any \ nelse" with anything + SHIFT + ENTER + else?

Or is there another way to introduce newlines without actually using javascript or replacing newlines with the newline key?

Note : elem is a content-accessible div.

+3


source to share


1 answer


Have you tried something like:

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

      

how

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

driver = webdriver.Chrome()
driver.get('http://foo.bar')

inputtext = 'foo\nbar'
elem = driver.find_element_by_tag_name('div')
for part in inputtext.split('\n'):
    elem.send_keys(part)
    ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()

      



ActionChains

after pressing there will be a chain key_down

SHIFT + ENTER + key_up

.

Likewise, you execute your SHIFT

+ ENTER

, then release the buttons so that you don't write everything to the caplock (due to SHIFT)

PS: this example adds too many new lines (due to a simple on loop inputtext.split('\n')

, but you got the idea.

+1


source







All Articles