Python / Selenium:在浏览器中粘贴得更快

时间:2017-07-21 11:16:44

标签: python selenium

我在python中编写了一个脚本,用于从一个浏览器中提取和粘贴400-500行文本。我正在使用send_keys()将文本内容放入文本区域。它是逐行写入(2行/秒),这导致几分钟完成操作。在Selenium中是否还有其他方法可以更快地写入(比如我们如何在1秒内手动粘贴)?

我的代码

<code>

import time
import re
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import ElementNotVisibleException
from selenium.webdriver.common.keys import Keys

def init_driver(uname,pwd):
    driver = webdriver.Chrome()
    driver.wait = WebDriverWait(driver, 5)
    driver.get("https://ops.stg1.xxxxxyyyxxxx.com/login.jsp")
    box = driver.wait.until(EC.presence_of_element_located((By.NAME, "j_username")))
    box.send_keys(uname)
    box = driver.wait.until(EC.presence_of_element_located((By.NAME, "j_password")))
    box.send_keys(pwd)
    button = driver.wait.until(EC.element_to_be_clickable((By.NAME, "login")))
    button.click()
    return driver

def copy():
    with open("Tag_input.txt") as f:
        for line in f:
            url = line.strip()  
            driver.get(url)
    k=re.findall('\=(\d+)',url)
    print(k[0])
    a=k[0]
    driver.wait = WebDriverWait(driver, 10)
    time.sleep(10)
    PC = driver.find_elements_by_xpath("//textarea[@name='messagingMap.PRIMARY_CONTENT.message']")
    PC.send_keys(Keys.CONTROL, "a")
    PC.send_keys(Keys.CONTROL, "c")
    print("Copied Primary content !!")
    for tag in PC:
        varPC = tag.text
        url1 = "http://jona.ca/blog/unclosed-tag-finder"
        driver.get(url1)
        driver.wait = WebDriverWait(driver, 10)
        time.sleep(10)

        text_area = driver.find_element_by_id("unclosed-tag-finder-input")
        text_area.send_keys(Keys.CONTROL, "v")
        button = driver.find_element_by_xpath("//input[@value='Submit']")
        button.click()

        result = driver.find_element_by_xpath("//pre[@id='unclosed-tag-finder-results']")
        res_list = list(result)
        print(res_list)
        op = result.text
        print(op)

        writer = csv.writer(open('Tag_OP.csv','a+'))
        z = zip(k,result)
        print(z)
        writer.writerows(k)
        writer.writerows(result)
        k = k.pop()
        print("List cleared",k[0])          
    driver.wait = WebDriverWait(driver, 10)
    time.sleep(10)
    return driver

if __name__ == "__main__":
    driver = init_driver("abdul.salam@xxxyyxxx.com","xxyyxx")
    copy()
    time.sleep(25)
    driver.quit()

</code>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Ctrl + A选择文本,按Ctrl + C复制文本,移动到新浏览器Ctrl + A选择目标字段中的所有文本(这样您就可以替换它),Ctrl + V粘贴。我可以想象它可能会更快,但我自己还没有完成任何基准测试。

当我进行搜索时,

This question突然弹出。它有更多细节,但是,例如,您的粘贴看起来像这样:

driver.find_element_by_id("unclosed-tag-finder-input").sendKeys(Keys.chord(Keys.CONTROL,"v"));