Hamed Baziyad
Hamed Baziyad

Reputation: 2019

How we can press Enter button in text-area by selenium python? (another way for submission)

I want to send a text to a field and submit it. HTML code was shown. How should I do? This is my HTML code:

<form class="_b6i0l"><textarea aria-label="Add a comment…" placeholder="Add a comment…" class="_bilrf" autocomplete="off" autocorrect="off" style="height: 18px;"></textarea>
<textarea aria-label="Add a comment…" placeholder="Add a comment…" class="_bilrf" autocomplete="off" autocorrect="off" style="height: 18px;"></textarea>
</form>

I want send a text to box with this code:

driver.execute_script("arguments[0].value = arguments[1]", driver.find_element_by_css_selector("textarea._bilrf"), "nice!")

How can I press enter to send my text? Could you help me, please?

Upvotes: 2

Views: 14847

Answers (2)

Hamed Baziyad
Hamed Baziyad

Reputation: 2019

from selenium.webdriver.common.keys import Keys
a = driver.find_element_by_css_selector("textarea._bilrf").send_keys("nice!")
a.submit()

Upvotes: 0

Alexey Dolgopolov
Alexey Dolgopolov

Reputation: 700

Try using send_keys() (https://seleniumhq.github.io/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html#selenium.webdriver.remote.webelement.WebElement.send_keys) and special keys module (https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html#module-selenium.webdriver.common.keys)

from selenium.webdriver.common.keys import Keys
...
driver.find_element_by_css_selector("textarea._bilrf").send_keys("nice!", Keys.ENTER)

Upvotes: 9

Related Questions