繁体   English   中英

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

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

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

我尝试了一些不同的方法。

执行javascipt:

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

但什么也没发生

我不能使用剪贴板,因为我的服务器没有 GUI。

这是我的 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>

已编辑

我想要不同于方法 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")

请分享您的一些代码,因为根据我的经验,send_keys 会在 1 秒内填充该区域

我的代码工作正常:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM