[英]How to create for loop to avoid Selenium error
我正在使用Selenium來自動執行任務。
手動任務需要某人發送批量發票。 有兩個部分:
Sectio 2(排隊等待發送電子郵件的發票)可以批量發送,但第1節(排隊等待打印傳遞的發票)需要通過單擊“電子郵件”按鈕單獨發送。
單擊“第1部分”的“電子郵件”按鈕后,將出現一個彈出窗口,並且需要單擊“發送發票”按鈕,然后在發送發票后將關閉彈出窗口。
第1節並非始終出現。 因此,當第1部分沒有電子郵件時,該部分不可見。 此部分的電子郵件數量也有所不同。
我以前遇到過StaleElementReferenceException錯誤,並設法通過獲取主頁的新元素來避免這種情況。
我現在遇到的問題是,如果第1節有5封電子郵件,我無法弄清楚我應該如何在腳本中執行腳本的循環操作,以便在彈出的窗口中單擊“發送發票”,然后單擊“發送發票”。返回主窗口,獲取新鮮元素,然后返回彈出窗口...
這是我的代碼:
### Do email run - Invoices Queued for Email Delivery ###
# Select the last table (Email delivery) and find the first checkbox and click
tables = driver.find_elements_by_class_name('fsmall')
tables[-1].find_element_by_css_selector("td:nth-child(1)").click()
# Click Do email run button
driver.find_element_by_name("email_queue").click()
# Wait for 50 seconds
time.sleep(50)
# Get page again once DOM loaded
driver.get(url)
# Find Invoices Queued for Print Delivery Section
tables = driver.find_elements_by_class_name('fsmall')
if 'Invoices Queued for Print Delivery' in [item.text for item in tables]:
### First loop
# Get table index of print delivery section
print_delivery_ind = [item.text for item in tables].index('Invoices Queued for Print Delivery')
# Get the table after Print Delivery table
idvdl_inv_tbl = tables[print_delivery_ind + 1]
# Get name of main window
main_window = driver.window_handles[0]
# Find the first invoice and click Email
idvdl_inv_tbl.find_element_by_link_text('Email').click()
# Wait for 3 seconds
time.sleep(3)
# Get name of the pop up window
popup_window = driver.window_handles[1]
# Switch to the pop up window
driver.switch_to_window(popup_window)
# Find the Send Invoice button and click
driver.find_element_by_name("submit_email").click()
# Switch to the main window
driver.switch_to_window(main_window)
### Second loop
# Get page again once DOM loaded
driver.get(url)
# Get all tables
tables = driver.find_elements_by_class_name('fsmall')
# Get table index of Print Delivery section
print_delivery_ind = [item.text for item in tables].index('Invoices Queued for Print Delivery')
# Get the table after Print Delivery table
idvdl_inv_tbl = tables[print_delivery_ind + 1]
# Get name of main window
main_window = driver.window_handles[0]
# Find the first invoice and click Email
idvdl_inv_tbl.find_element_by_link_text('Email').click()
# Wait for 3 seconds
time.sleep(3)
# Get name of the pop up window
popup_window = driver.window_handles[1]
# Switch to the pop up window
driver.switch_to_window(popup_window)
# Find the Send Invoice button and click
driver.find_element_by_name("submit_email").click()
driver.close()
如果有人能指出我正確的方向,將不勝感激。 謝謝。
嗯,只要表中有“排隊等待打印的發票”,就會有一個令人驚訝的小變化,它將使您的循環重復進行。 更改此行:
if 'Invoices Queued for Print Delivery' in [item.text for item in tables]:
至:
while 'Invoices Queued for Print Delivery' in [item.text for item in tables]:
然后,在循環體內刪除第二個元素collect-同時保持頁面重新加載和tables
的重新初始化。 所以這些行:
### First loop
# Get table index of print delivery section
print_delivery_ind = [item.text for item in tables].index('Invoices Queued for Print Delivery')
# -----
# the rest of the lines
# -----
# up until these - keep them, and nothing afterwards:
# Get page again once DOM loaded
driver.get(url)
# Get all tables
tables = driver.find_elements_by_class_name('fsmall')
因此,當頁面上有帶有該文本的表格時,您將循環播放。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.