2016-05-05 21 views
0

我有一個腳本,它使用剪貼板中的數據(包含文本的行)在Selenium的幫助下檢查它是否存在於網站中。如果找不到記錄,則打印該行的信息(爲了告訴我什麼是未找到的)。打印行並檢查該行是否在csv內

我現在想要實現的是,在網絡瀏覽器中找不到的行也檢查它是否存在於我的筆記本電腦中的CSV中。所以最終的結果應該是:print(row)+在CSV中或不在CSV中。

我創建了下面的腳本,但它不像預期的那樣工作。用###提到了從原始文件添加到新腳本的內容。

你能給我一些提示,讓它工作正常。謝謝。

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import time 
import csv 
import tkinter as tk 

path_to_Ie = 'C:\\Python34\\ChromeDriver\\ChromeDriver.exe' # change path as needed 
browser = webdriver.Chrome(executable_path = path_to_Ie) 
url = 'https://wwww.test.corp/' 
browser.get(url) 

browser.find_element_by_xpath("//*[@id='username']").send_keys("xxxx") 
browser.find_element_by_xpath("//*[@id='password']").send_keys("xxxx") 

browser.find_element_by_xpath("//*[@id='login-link']").click() 

browser.get('https://test.com/troubleshoot#displayall=true') 
browser.find_element_by_xpath("//*[@id='content-column']/div[4]/form/div[1]/span/label").click() 

root = tk.Tk() 
# keep the window from showing 
root.withdraw() 
# read the clipboard 

today = (time.strftime("%d_%m_%Y")) 
file = 'Z:\\deleted_%s.csv' % today 

rows = root.clipboard_get().split('\n') 
rows = [row.strip() for row in rows] 

validation = "" 
while validation != "Select All (1)": 

    for row in rows: 
     browser.get('https://test.com/troubleshoot#displayall=true') 
     time.sleep(2) 
     browser.find_element_by_xpath("//*[@id='agent_list_filter_id_2']").clear() 
     browser.find_element_by_xpath("//*[@id='agent_list_filter_id_2']").send_keys(row) 
     time.sleep(2) 
     browser.find_element_by_xpath("//*[@id='content-column']/div[4]/form/div[1]/span/span[1]").click() 
     time.sleep(2) 
     validation = browser.find_element_by_xpath("//*[@id='action-select-all']/span/span").text 
     if validation != "Select All (1)": # or anything that is a falsy validation 
      browser.get('https://test.com/troubleshoot#displayall=true') 
      time.sleep(1) 
      browser.find_element_by_xpath("//*[@id='content-column']/div[4]/form/div[1]/span/label").click() 
      time.sleep(1) 
      print(row) ###### below is what I added from the original########## 
      with open(file, 'rt') as f: 
       reader = csv.reader(f, delimiter=',') 
       for row in reader: 
       for field in row: 
       if field == row: 
        print ("%s exist in the CSV") % row 
      continue ###### above is what I added from the original########## 
     time.sleep(2) 
     browser.find_element_by_xpath("//*[@id='action-select-all']/span/span").click() 
     time.sleep(2) 
     browser.find_element_by_xpath("//*[@id='action-delete']/span/span").click() 
     time.sleep(2) 
     browser.find_element_by_xpath("//*[@id='btn_save']").click() 
    else: 
     break 
+0

它究竟是不是工作?我看到的一個錯誤是,你需要使用'print(「%s存在於CSV中)%row'來打印'row' - 代碼中缺少'%'字符後的's'。 – martineau

+0

Ups,已經修復。問題是,似乎這些新的行添加不會改變任何東西..代碼作爲以前.. – Gonzalo

回答

0

該問題很可能是因爲您有兩個變量名爲row正在同時使用。其中一個是由外部for row in rows:聲明確定的,另一個聲明是由內部for row in reader:聲明創建的。

要修復它,請重命名其他內容,如for csvrow in reader:,並相應地更改對其的所有引用。

+0

你是對的。非常感謝。現在運作良好! – Gonzalo

+0

只是最後一個問題,如果我有2 csv檢查而不是1。有什麼簡單的方法可以做到這一點?而不是重複:開放......。非常感謝 – Gonzalo

+0

我可以考慮一些方法來優化對兩個(或多個)csv文件的同時檢查,但不會顯着。您當前的實現速度很慢,即使檢查一個,因爲它會讀取並處理每個字段剪貼板中每行的整個文件的行。由於該文件在做這件事時可能沒有改變,我會在內存中緩存來自csv文件的必要信息,這將大大加快速度。這樣做之後,添加另一個csv來檢查相同的方式 - 使用緩存信息 - 可能不會成爲問題。 – martineau