我是python的新手,在让脚本执行我需要的工作时遇到了一些困难,我需要一些帮助。我的代码可能也可以排序得更好,如果它很乱,我感到抱歉。整个脚本只是使手动任务自动化。步骤如下:
所有这些当前都有效,但是当整个脚本完成时,我希望它返回到步骤3,但是这次是读取文本文件中的第2行。在读取第3行之前,请再次执行步骤3-8,依此类推。
这个想法是,我有一个.txt文件,其中包含许多电子邮件地址,每行一个,其中我的脚本在每个地址上都通过此过程。 如果可能的话,我还希望脚本在读取后在每行后面附加“-成功”。因此,脚本将运行x次,具体取决于test.txt中的行数 如果可能的话,最好也进行一些错误处理,即如果有任何错误,请移至文件的下一行。
这是我的完整代码,我不得不删除URL和一些敏感值,我希望它是可以理解的。我遇到问题的部分在下面突出显示:
# coding=UTF-8
import clipboard
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
def login():
## Define logins for#
usernameB = ''
passwordB = ''
userMaestro = ''
passMaestro = ''
driver.switch_to_window(driver.window_handles[0])
driver.get(first_url)
## Perform login in B ##
username = driver.find_element_by_id('ctl00_logincontent_username_f')
username.send_keys(usernameB)
password = driver.find_element_by_id('ctl00_logincontent_password_f')
password.send_keys(passwordB)
loginButton = driver.find_element_by_id('ctl00_logincontent_submit')
loginButton.click() ##
driver.switch_to_window(driver.window_handles[1])
driver.get(second_url)
#### Perform login in Maestro
usernameM = driver.find_element_by_name('username')
usernameM.send_keys(userMaestro)
passwordM = driver.find_element_by_name('password')
passwordM.send_keys(passMaestro)
loginMaestro = driver.find_element_by_name('submit')
loginMaestro.click()
def searchB():
driver.switch_to_window(driver.window_handles[0]) # Use first tab
## Search function ##
emailSearch = driver.find_element_by_id('quicksearchinput')
with open('C:\\Users\\user\\test.txt','r') as f:
for email in f:
emailSearch.send_keys(email, Keys.RETURN)
sendSMS()
def sendSMS():
try:
kundenummer = driver.find_elements_by_tag_name('dd')[2].text # Grabs value from instersting tag.
telefon = driver.find_elements_by_tag_name('dd')[4].text # Grabs value from interesting tag.
except NoSuchElementException as exception:
print "Element not found. Getting next email"
searchBrikks()
mailerText = "He"
## SEND SMS ##
driver.switch_to_window(driver.window_handles[1])
driver.get(second_url)
driver.find_element_by_name('til').send_keys(telefon)
driver.find_element_by_id('msgtxt').send_keys(mailerText)
driver.find_element_by_xpath('//input[@value="Send SMS"]').click() ## We use this to send the SMS once done.
smsRef = driver.find_elements_by_tag_name('strong')[1].text # Copies the output SMS-Ref.
print('Successfully sent SMS and copied the referance number for log. ')
addnoteBrikks()
def addnoteBrikks():
smsRef = driver.find_elements_by_tag_name('strong')[1].text # Copies the output SMS-Ref.
driver.switch_to_window(driver.window_handles[0]) # Goes to B
kundenummer = driver.find_elements_by_tag_name('dd')[2].text # Grabs ID for URL
telefon = driver.find_elements_by_tag_name('dd')[4].text # Grabs value from interesting tag.
driver.get(third_url+kundenummer) # Goes to "add note" on customer in B
fullDraft = 'SMS sendt til:'+ telefon + '. Kundenummer oppgitt: ' +kundenummer + '\n' + smsRef
shortDraft = 'Mailer-daemon, SMS Sendt. Ref:' +str(smsRef) #For subject in Brikks. Add SMS-ref?
emneBrikks = driver.find_element_by_id('ctl00_maincontent_uiSubject_f')
emneBrikks.send_keys(shortDraft)
bodyBrikks = driver.find_element_by_id('ctl00_maincontent_uiBody_f')
bodyBrikks.send_keys(fullDraft)
createTicket = driver.find_element_by_id('ctl00_maincontent_save')
createTicket.click() #Create the ticket in Brikks
searchB()
first_url = "https://"
second_url = "http://"
third_url = "https:"
driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.execute_script('window.open("{}");'.format(''))
def main():
login()
searchB()
driver.switch_to_window(driver.window_handles[1])
smsRef = driver.find_elements_by_tag_name('strong')[1].text # Copies the output SMS-Ref.
if __name__ == '__main__':
main()
如何在这里实现我想要的?感谢任何帮助!
编辑:
所以我感兴趣的部分是这样:
def searchBrikks():
driver.switch_to_window(driver.window_handles[0]) # Use first tab
## Search function ##
emailSearch = driver.find_element_by_id('quicksearchinput')
with open('C:\\Users\\user\\test.txt','r') as f:
for email in f:
emailSearch.send_keys(email, Keys.RETURN)
sendSMS()
### What to write further here? ###
下次调用searchB()时,如何读取第2行?
答案 0 :(得分:0)
我首先看到的是,每次 searchB()
被调用,您就遍历了test.txt文件的全部内容。
with open('C:\\Users\\user\\test.txt','r') as f:
for email in f:
直到它达到EOF为止,都不要停止。不必将循环放在searchB()
函数中,而可以将其放在主要函数中,那样,您可以拥有更多控制权,并在所需的test.txt文件的任何行上调用searchB()
。
对于日志记录,您需要查看logging库,该库将允许您指定日志文件,并在写入某些内容或调用失败后放置日志消息。将- successful
附加到各行的末尾会很烦人。
答案 1 :(得分:0)
我通过将循环移动到主要函数来解决了这个问题:
def main():
driver.execute_script('window.open("{}");'.format(''))
login()
driver.switch_to_window(driver.window_handles[0]) # Use first tab
emailSearch = driver.find_element_by_id('quicksearchinput') # Identify Quicksearch text-field
with open('C:\\Users\\user\\test.txt','r+') as f:
for email in f:
searchB(email) ## <-- Added this
print(email)
使用我的searchB()函数是这样的:
def searchB(mail): ## <-- Added this
driver.switch_to_window(driver.window_handles[0]) # Use first tab
emailSearch = driver.find_element_by_id('quicksearchinput') # Identify Quicksearch text-field
emailSearch.send_keys(email, Keys.RETURN)
sendSMS()