Home > Back-end >  Send_keys long text so slowly (Python Selenium)
Send_keys long text so slowly (Python Selenium)

Time:05-16

I'm using element.send_keys("Long text") to fill textarea, but it takes very much time and the script has to fill it in fast.

I tried some different method.

Execute javascipt:

browser.execute_script('document.getElementsByClassName("inputarea")[0].value="Long text"')

but nothing happened

I can't using clipboard because my server doesn't have GUI.

Here is my HTML:

<div data-mprt="3"  style="width: 954px; height: 268px;"><div  style="position: absolute; will-change: transform; top: 0px; height: 268px; width: 68px;" role="presentation" aria-hidden="true"><div  style="left: 0px; width: 0px; height: 268px;"></div><div  style="position: absolute;" role="presentation" aria-hidden="true"></div><div  style="position: absolute; width: 68px; font-family: &quot;Droid Sans Mono&quot;, &quot;monospace&quot;, monospace, &quot;Droid Sans Fallback&quot;; font-weight: normal; font-size: 14px; line-height: 20px; letter-spacing: 0px; height: 268px;" role="presentation" aria-hidden="true"><div style="position:absolute;top:0px;width:100%;height:20px;"><div  style="width:68px; height:20px;"></div><div  style="left:0px;width:42px;">1</div></div></div></div><div  role="presentation" style="position: absolute; overflow: hidden; left: 68px; width: 886px; height: 268px;" data-mprt="5"><div  style="position: absolute; overflow: hidden; width: 1000000px; height: 1000000px; touch-action: none; will-change: transform; top: 0px; left: 0px;"><div  style="position: absolute; height: 0px; width: 886px;" role="presentation" aria-hidden="true"><div style="position:absolute;top:0px;width:100%;height:20px;"><div  style="width:886px; height:20px;"></div></div></div><div role="presentation" aria-hidden="true" ></div><div  style="position: absolute;" role="presentation" aria-hidden="true"></div><div  style="position: absolute; font-family: &quot;Droid Sans Mono&quot;, &quot;monospace&quot;, monospace, &quot;Droid Sans Fallback&quot;; font-weight: normal; font-size: 14px; line-height: 20px; letter-spacing: 0px; width: 886px; height: 268px;" role="presentation" aria-hidden="true" data-mprt="7"><div style="top:0px;height:20px;" ><span><span>&nbsp;</span></span></div></div><div data-mprt="1"  style="position: absolute; top: 0px;"><div  title="Show Fixes (Ctrl .)" style="position: absolute; visibility: hidden; max-width: 886px;" widgetid="LightBulbWidget"></div></div><div role="presentation" aria-hidden="true" ><div  style="height: 20px; top: 0px; left: 0px; font-family: &quot;Droid Sans Mono&quot;, &quot;monospace&quot;, monospace, &quot;Droid Sans Fallback&quot;; font-weight: normal; font-size: 14px; line-height: 20px; letter-spacing: 0px; display: block; visibility: hidden; width: 2px;"></div></div></div><div role="presentation" aria-hidden="true"  style="position: absolute; width: 880px; height: 10px; left: 0px; bottom: 0px;"><div  style="position: absolute; top: 0px; left: 0px; height: 10px; will-change: transform; width: 880px;"></div></div><canvas  style="position: absolute; will-change: transform; top: 0px; right: 0px; width: 6px; height: 268px;" aria-hidden="true" width="6" height="268"></canvas><div role="presentation" aria-hidden="true"  style="position: absolute; width: 6px; height: 268px; right: 0px; top: 0px;"><div  style="position: absolute; top: 0px; left: 0px; width: 6px; will-change: transform; height: 268px;"></div></div></div><div role="presentation" aria-hidden="true" style="width: 954px;"></div><textarea data-mprt="6"  autocorrect="off" autocapitalize="none" autocomplete="off" spellcheck="false" aria-label="Editor content;Press Alt F1 for Accessibility Options." role="textbox" aria-multiline="true" aria-haspopup="false" aria-autocomplete="both" style="font-size: 1px; line-height: 20px; top: 0px; left: 68px; width: 0px; height: 0px;" wrap="off"></textarea><div style="position: absolute; top: 0px; left: 0px; width: 0px; height: 0px;"></div><div data-mprt="4"  style="width: 954px;"><div  style="display: none; position: absolute;" role="dialog" aria-hidden="true" widgetid="editor.contrib.accessibilityHelpWidget"><div role="document"></div></div></div><div data-mprt="8"  style="position: absolute; left: 0px; width: 0px; height: 268px;" role="presentation" aria-hidden="true"><div  style="height: 268px;"></div><canvas style="position: absolute; left: 0px; width: 1px; height: 268px;" width="1" height="268"></canvas><div style="position: absolute; will-change: transform; width: 0px;" ><div style="position: absolute; width: 0px; height: 0px;" ></div></div></div></div>

CodePudding user response:

Try the below way:

 ((JavascriptExecutor) driver).executeScript("arguments[0].value = arguments[1];", driver_tmp.findElement(By.id("text_id")), text);

CodePudding user response:

Maybe, it can be work to use XPATH.

textarea_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div/textarea'))).send_keys("Long text")

CodePudding user response:

Please share a little bit of your code cause send_keys fills the area under 1 second in my experience

My code just works fine :

import selenium
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

browser = webdriver.Firefox()
browser.get('YOUR_URL')
area = browser.find_element(by=By.<Your Choice>, value=<Name or Xpath or ...>)
area.send_keys('Your Text')
  • Related