Python 如何创建for循环以避免Selenium错误
我正在使用Selenium自动化一项任务 手动任务需要有人发送批量发票。 分为两部分: 排队等待打印交付的发票 排队等待电子邮件送达的发票 第2节排队等待电子邮件发送的发票可以批量发送,但第1节排队等待打印发送的发票需要通过单击电子邮件按钮单独发送 单击第1节的此电子邮件按钮后,将出现一个弹出窗口,需要单击“发送发票”按钮,然后发送发票后弹出窗口将关闭 第1节并非总是出现。因此,当没有第1节的电子邮件时,此节不可见。这一部分的电子邮件数量各不相同 我以前遇到过StaleElementReferenceException错误,并通过获取主页的新元素来避免它 我现在遇到的问题是,如果第1节有5封电子邮件,我无法在我的脚本中找出我应该如何或在哪里为循环执行此操作,以便它将在弹出窗口中单击发送发票并返回主窗口,获取新鲜元素并返回弹出窗口 这是我的代码:Python 如何创建for循环以避免Selenium错误,python,selenium,Python,Selenium,我正在使用Selenium自动化一项任务 手动任务需要有人发送批量发票。 分为两部分: 排队等待打印交付的发票 排队等待电子邮件送达的发票 第2节排队等待电子邮件发送的发票可以批量发送,但第1节排队等待打印发送的发票需要通过单击电子邮件按钮单独发送 单击第1节的此电子邮件按钮后,将出现一个弹出窗口,需要单击“发送发票”按钮,然后发送发票后弹出窗口将关闭 第1节并非总是出现。因此,当没有第1节的电子邮件时,此节不可见。这一部分的电子邮件数量各不相同 我以前遇到过StaleElementRefere
### 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]:
致:
然后在循环体内部,移除第二个元素聚集,同时保持页面重新加载和表的重新初始化。所以这些线:
### 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')
因此,当页面上有包含该文本的表格时,您将进行循环。if部分中的代码是否试图对打印传递中的项目进行两次循环?我这样问是因为有这样一个注释,Get table index of Email Delivery,但接下来的五行也会获取排队等待打印递送表的发票-注释与代码相矛盾?还有,现在你没有看到StaleElementReferenceException,对吧,你只是想构造一个更坚固的循环?@Todormanakov抱歉,我在那里编辑了我的代码。“如果”部分试图确定“打印传递”部分是否可用。确切地说,我没有看到错误,但我希望有一个更可靠的循环,使这些代码更健壮。谢谢。哦,孩子@DebanjanB,请不要仅仅通过浏览标题和看到你的关键词来结束问题-在这种情况下,StaleElementReferenceException。这个问题与StaleElementReferenceException没有任何关系,OP做了很多非常有趣的事情只是为了不击中它。现在,如果您愿意,请将您的标记恢复为副本。我有一个半页长的答案,不会出现在评论中。谢谢@Todormanakov,我将在周一测试这段代码!有趣的是,我有一个与while-loop类似的脚本,但这次我的头甚至没有想到while-loop:D
### 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')