简体   繁体   English

Send_keys 长文本这么慢(Python + Selenium)

[英]Send_keys long text so slowly (Python + Selenium)

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.我正在使用 element.send_keys("Long text") 来填充 textarea,但这需要很长时间,并且脚本必须快速填充它。

I tried some different method.我尝试了一些不同的方法。

Execute javascipt:执行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.我不能使用剪贴板,因为我的服务器没有 GUI。

Here is my HTML:这是我的 HTML:

<div data-mprt="3" class="overflow-guard" style="width: 954px; height: 268px;"><div class="margin" style="position: absolute; will-change: transform; top: 0px; height: 268px; width: 68px;" role="presentation" aria-hidden="true"><div class="glyph-margin" style="left: 0px; width: 0px; height: 268px;"></div><div class="margin-view-zones" style="position: absolute;" role="presentation" aria-hidden="true"></div><div class="margin-view-overlays" 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 class="current-line" style="width:68px; height:20px;"></div><div class="line-numbers lh-even" style="left:0px;width:42px;">1</div></div></div></div><div class="monaco-scrollable-element editor-scrollable vs-dark" role="presentation" style="position: absolute; overflow: hidden; left: 68px; width: 886px; height: 268px;" data-mprt="5"><div class="lines-content monaco-editor-background" style="position: absolute; overflow: hidden; width: 1000000px; height: 1000000px; touch-action: none; will-change: transform; top: 0px; left: 0px;"><div class="view-overlays" style="position: absolute; height: 0px; width: 886px;" role="presentation" aria-hidden="true"><div style="position:absolute;top:0px;width:100%;height:20px;"><div class="current-line" style="width:886px; height:20px;"></div></div></div><div role="presentation" aria-hidden="true" class="view-rulers"></div><div class="view-zones" style="position: absolute;" role="presentation" aria-hidden="true"></div><div class="view-lines" 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;" class="view-line"><span><span>&nbsp;</span></span></div></div><div data-mprt="1" class="contentWidgets" style="position: absolute; top: 0px;"><div class="lightbulb-glyph" title="Show Fixes (Ctrl+.)" style="position: absolute; visibility: hidden; max-width: 886px;" widgetid="LightBulbWidget"></div></div><div role="presentation" aria-hidden="true" class="cursors-layer cursor-line-style cursor-solid"><div class="cursor " 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" class="invisible scrollbar horizontal" style="position: absolute; width: 880px; height: 10px; left: 0px; bottom: 0px;"><div class="slider" style="position: absolute; top: 0px; left: 0px; height: 10px; will-change: transform; width: 880px;"></div></div><canvas class="decorationsOverviewRuler" 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" class="invisible scrollbar vertical" style="position: absolute; width: 6px; height: 268px; right: 0px; top: 0px;"><div class="slider" 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" class="inputarea" 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" class="overlayWidgets" style="width: 954px;"><div class="accessibilityHelpWidget" style="display: none; position: absolute;" role="dialog" aria-hidden="true" widgetid="editor.contrib.accessibilityHelpWidget"><div role="document"></div></div></div><div data-mprt="8" class="minimap slider-mouseover" style="position: absolute; left: 0px; width: 0px; height: 268px;" role="presentation" aria-hidden="true"><div class="minimap-shadow-hidden" 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;" class="minimap-slider"><div style="position: absolute; width: 0px; height: 0px;" class="minimap-slider-horizontal"></div></div></div></div>

Edited已编辑

I want the method different from the method send_keys(), because send_keys() so slow.我想要不同于方法 send_keys() 的方法,因为 send_keys() 太慢了。

试试下面的方法:

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

也许,使用 XPATH 是可行的。

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

Please share a little bit of your code cause send_keys fills the area under 1 second in my experience请分享您的一些代码,因为根据我的经验,send_keys 会在 1 秒内填充该区域

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium python 通过 send_keys 发送长文本 - how to send long text with send_keys using selenium python 替代 send_keys() - selenium python - Alternative to send_keys() - selenium python Python Selenium send_keys() 文本文件内空白区域的错误原因 - Python Selenium send_keys() error cause of the blank area inside the text file Python Selenium:输入文本框,send_keys 不起作用(angular javascript??) - Python Selenium: input textbox, send_keys not working (angular javascript??) Selenium-send_keys()发送不完整的字符串 - Selenium - send_keys() sending incomplete string Send_keys函数确实按预期在Selenium python中工作 - Send_keys function does do its work as expected in Selenium python 用 javascript 执行脚本 python Z8E00596AD8DE22513FF8F8D8478 替换 xpath 找到的元素的 send_keys() - Replace send_keys() for an element found by xpath with javascript exec script python selenium 无法使用Selenium Webdriver中的send_keys上传文件 - Unable to Upload File using send_keys in Selenium Webdriver 在 selenium iframe 元素上触发 JavaScript 自定义事件,因为 send_keys 输入值不持久 - Trigger JavaScript custom event on selenium iframe element, as send_keys input values not persisting 在python selenium javascript中慢慢滚动 - Slowly scroll in python selenium javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM