所以我有一个路由号码的文本文件,我需要检查其有效性,但我不想手动执行。现在,我有一个 GUI,我正在将每个文件输入其中(这就是代码在那里的原因),但计划是将文本文件转换为列表,然后让我的程序查找每个文件。不过,我无法一次发送所有内容,因为我需要在每次搜索之间刷新浏览器(或清除搜索栏)。使用此代码,我将文本文件转换为列表:
with open('C:/users/ME/desktop/routingnumbers') as f:
lines = f.read().splitlines()
如何让我的程序一次发送一个列表项,然后重新开始?这是我当前的计划。
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.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
import time
from tkinter import *
window = Tk()
window.title("Routing Number Lookup")
window.configure(background="light grey")
window.geometry('300x250')
Routing = Label(window, text="Routing Number: ", font=("Arial"), fg="gray17")
Routing.grid(column=0, row=0, sticky='e')
Routing.configure(background="light grey")
RoutingEntry = Entry(window, width=20)
RoutingEntry.configure(background="light grey")
RoutingEntry.grid(column=1, row=0)
driver = webdriver.Chrome()
driver.get('http://www.routingnumber.com/')
def RoutingSearch():
SearchBar = driver.find_element_by_xpath("/html/body/div[1]/form[1]/div[1]/input[1]")
RoutingInput = (RoutingEntry.get())
SearchBar.send_keys(RoutingInput)
SearchBar.send_keys(Keys.ENTER)
time.sleep(0.5)
ValidRouting = driver.find_element_by_xpath("/html/body/div[1]/form[1]/div[1]/div/div[1]").text
BankName = driver.find_element_by_xpath("/html/body/div[1]/form[1]/div[1]/div/table[1]/tbody/tr[1]/td[2]/a").text
print("\n")
print(ValidRouting, BankName)
driver.refresh()
SearchButton = Button(window, text="Search", command=RoutingSearch)
SearchButton.grid(column=1, row=3)
SearchButton.configure(background = "light grey")
window.mainloop()
最佳答案
如果您只需要验证路由号码,请使用以下命令:
def validate_routing(rn):
if len(rn) != 9:
return False
n = 0
for i in range(0, len(rn), 3):
n += int(rn[i]) * 3
n += int(rn[i + 1]) * 7
n += int(rn[i + 2])
return n != 0 and n % 10 == 0
with open('routing.txt') as f:
for rn in f:
rn = rn.rstrip()
print(rn, validate_routing(rn))
<小时/>
关于python - 尝试一次通过搜索发送一个列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60346232/