2017-07-21 73 views
1

我已經在python中編寫了一個腳本,可以將400-500行文本從一個瀏覽器粘貼到另一個瀏覽器。我正在使用send_keys()將文本內容放入文本區域。它正在逐行寫入(2行/秒),這會導致完成操作幾分鐘。 Selenium有沒有其他方法可以更快寫入(比如我們如何在1秒內手動進行粘貼)?Python/Selenium:在瀏覽器中粘貼速度更快

我的代碼

<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("[email protected]","xxyyxx") 
    copy() 
    time.sleep(25) 
    driver.quit() 

</code> 

回答

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")); 
+0

探微ü提到,做了一些研究,發現這個(Keys.CONTROL,「C」)的問題,但它沒有爲我工作.. – Abdul

+0

好的...它怎麼不起作用?除非您提供更多信息,否則我們無法幫助您。 –

+0

當我使用(Keys.CONTROL,「c」)時顯示縮進錯誤..我在Windows 7專業版中使用python 3.5版本。 – Abdul

相關問題