我有一個腳本,它使用剪貼板中的數據(包含文本的行)在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
它究竟是不是工作?我看到的一個錯誤是,你需要使用'print(「%s存在於CSV中)%row'來打印'row' - 代碼中缺少'%'字符後的's'。 – martineau
Ups,已經修復。問題是,似乎這些新的行添加不會改變任何東西..代碼作爲以前.. – Gonzalo