如何创建for循环,以避免错误硒

问题描述 投票:0回答:1

我使用硒来自动执行任务。

手动任务需要有人发送大量的发票。有两个部分:

  1. 发票排队等待打印发货
  2. 发票排队等待电子邮件传送

第2节(发票排队等待电子邮件传送)可以批量发送,但第1(发票排队等待打印输出),需要通过点击电子邮件按钮单独发送。

一旦这个电子邮件按钮被点击的第1节,一个弹出窗口将出现,并且需要点击,然后在弹出窗口将关闭,一旦发票被送到一个发送发票按钮。

第1节不会出现所有的时间。因此,当存在于第1节没有电子邮件,这部分是不可见的。和电子邮件的这一部分的数量而变化。

我以前也遇到过StaleElementReferenceException错误,已成功通过获取的主网页的新鲜元素,以避免它。

那我现在遇到的问题是,如果第1有5封电子邮件,我无法弄清楚如何或在哪里我的剧本,我应该for循环这样做,这样它会点击发送发票在弹出窗口和回到主窗口,取新鲜的元素,并返回到弹出窗口...

这是我的代码:

### 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()

将不胜感激,如果有人可以点我到正确的方向。谢谢。

python selenium
1个回答
1
投票

那么有一个令人惊讶的小的变化,这将使你的循环迭代,只要有“发票排队等待打印输出”的表格中。改变这一行:

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]:

然后循环的体内,取出第二个元素收集 - 同时保持页面重载,并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')

这样的话你会循环,而没有与页面上的文本的表格。

© www.soinside.com 2019 - 2024. All rights reserved.