我想用python語言編寫一個測試用於管理用戶的網頁的硒。在此頁面中,有人可以爲用戶添加角色,如果在添加角色時存在角色,則會引發警報。我不知道警報是否是JavaScript警報或網頁的元素。我想自動檢查警報是否存在,因爲檢查列表中的角色會浪費時間並且負載很重。我試過這個:檢查是否有任何警報存在使用硒python
browser = webdriver.Firefox()
browser.get("url")
browser.find_the_element_by_id("add_button").click()
try:
alert = browser.switch_to_alert()
alert.accept()
print "alert accepted"
except:
print "no alert"
但它沒有工作,我得到了「UnexpectedAlertPresentException」。 我也試過這個:
browser = webdriver.Firefox()
browser.get("url")
browser.find_the_element_by_id("add_button").click()
s = set(browser.window_handles)
s.remove(browser.current_window_handle)
browser.switch_to_window(s.pop())
但我得到了同樣的例外。 此外,我試圖訪問與螢火蟲警報,以檢查我是否可以訪問其屬性,但右鍵點擊被禁用。 即使在其他語言中,我也需要一個解決方案。無論如何,我可以理解這種方法。 我會感謝任何幫助。
您是否嘗試過點擊和switch_to_alert之間的短暫延遲? – Richard
還沒有,謝謝你的建議@理查德。我想我可以使用A.R.的代碼。給我 –
@Richard必須在'switch_to_alert()'到'accept()'之間有一個延遲?否則可能會出現問題,你認爲? – 2014-07-25 09:05:42