我正在使用 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: "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; 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: "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; 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> </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: "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; 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>
uj5u.com熱心網友回復:
試試下面的方法:
((JavascriptExecutor) driver).executeScript("arguments[0].value = arguments[1];", driver_tmp.findElement(By.id("text_id")), text);
uj5u.com熱心網友回復:
也許,使用 XPATH 是可行的。
textarea_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div/textarea'))).send_keys("Long text")
uj5u.com熱心網友回復:
請分享您的一些代碼,因為根據我的經驗,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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/475657.html
標籤:javascript Python 硒 硒网络驱动程序
