python - 如何减少 Python selenium Pyperclip.paste 执行时间

标签 python selenium selenium-chromedriver

我正在使用以下 python 脚本从本地文件复制内容并将其粘贴到文本区域,这实际上工作得很好。但是,完成此任务需要花费大量时间,因为我的脚本将文件内容一一键入到文本区域。

所以我在寻找是否有一种方法可以修改以下脚本,以便它一次性粘贴完整的数据。

import selenium
import pyperclip
path = 'path\to-my-file\Customer - Copy.txt'
fo = open(path, 'r').read()

#fo = fo.replace("\r","")
#fo = fo.replace("\n","")

pyperclip.copy(fo)
from selenium import webdriver
from selenium.webdriver import ActionChains
#from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
chromedriver = 'path\to-my-file\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)

import time 

time.sleep(10)

browser.get('http://example.com')

time.sleep(10)

username1 = browser.find_element_by_class_name("ace_text-input")

username1.send_keys(pyperclip.paste())

最佳答案

如果您的目标仅仅是任务自动化,您可以通过编写函数并传递元素和设置文本来加快输入速度直接用脚本注入(inject)字段如下:

def set_text(element, text):
  element._parent.execute_script("""
    var elm = arguments[0], text = arguments[1];
    if (!('value' in elm))
      throw new Error('Expected an <input> or <textarea>');
    elm.focus();
    elm.value = text;
    elm.dispatchEvent(new Event('change'));
    """, element, text)

关于python - 如何减少 Python selenium Pyperclip.paste 执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804541/

相关文章:

python - 用python收集大量数据

Python 保护设置 IE

python - 获取所有子元素

java - Selenium 使用当前 session 重新打开浏览器

webdriver - Chromedriver 与 webdriverio - init 上未处理的请求

javascript - heroku python 和突出显示 js 不工作

python - 从文本字符串解析数据对象结构

python - 行到列csv python用于一个标题下的多个值

docker compose 中的 Selenium 独立 - 被操作系统杀死?

python - 如何循环浏览并点击相关项目