如何在python中输入从txt文件到变量的文本列表


activation = driver.find_element(By.XPATH, '//*[@id="__layout"]/div/aside/div/div[1]/section[1]/div[1]/div/div/form/div[2]/div/span')
code = driver.find_element(By.XPATH, '//*[@id="__layout"]/div/aside/div/div[1]/section[1]/div[1]/div/div/form/div/input')
list = open("C:UsersinfinDesktopList.txt", "r")

只要activation为true,我就想将list.txt中的代码列表输入到code中,并根据列表中的元素数量创建一个循环

例如,如果列表有5个代码,我想看看activation是否为真。如果是真的,它将超时120秒(我可以做这部分(120秒后,它将输入从list.txtcode的代码

我仍在学习python,我正在尝试不同的东西,并在python 中学习新单词

我不知道我是否理解你的问题,但如果你的代码是分开的,那么你可以进行

all_codes = open("C:\Users\infin\Desktop\List.txt", "r").read().split('n')

更好地在路径中使用\\,因为\用于特殊字符,如n(新行(、t(制表符(等。即使在路径中,单个\也会造成问题。或使用前缀r作为raw string

all_codes = open(r"C:UsersinfinDesktopList.txt", "r").read().split('n')

接下来,您需要for-循环来获得单个代码,将其发送到输入并检查结果。

from selenium.webdriver.common.keys import Keys
for value in all_codes:
code = driver.find_element_xpath('//*[@id="__layout"]//section[1]/div[1]//form/div/input')
code.send_key(value)
code.send_key(Keys.ENTER)
activation = driver.find_element_xpath('//*[@id="__layout"]//section[1]//form/div[2]/div/span')
if activation.text == '... some text ...':
break # exit loop before end of codes

您可能需要在每个循环中再次使用find_element,因为find_element引用了内存中的对象,当您放置code时,它可能会更改HTML,并且元素可以移动到内存中的不同位置。

您也可以尝试使用//classesids查找较短的xpath

最新更新