我使用剪贴板中的数据(带文本的行)来检查Selenium是否存在于网站中的脚本。如果找不到该记录,则打印该行的信息(以便告诉我未找到的内容)。
我现在想要实现的是,在网络浏览器中找不到的行也会检查我的笔记本电脑中是否存在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 :(得分:0)
问题可能是因为你有两个名为row
的变量同时被使用。一个定义是由外部for row in rows:
语句定义的,而另一个定义是在内部for row in reader:
语句中创建的。
要解决此问题,请将内部内容重命名为其他内容,例如for csvrow in reader:
,并相应地更改对它的所有引用。