3 回答

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
當(dāng)這種情況發(fā)生時(shí),您至少應(yīng)該在斷言中發(fā)布警告。這樣你就知道發(fā)生了什么錯(cuò)誤。如果你這樣做了,以下內(nèi)容會(huì)對(duì)你有所幫助......
在頁面初始化時(shí)或在它開始加載相關(guān)頁面時(shí)添加它。你也可以在任何頁面上這樣做,真的。
driver.execute_script('''
window.errorCount = 0;
window.onerror = function (error, url, line, column, errorMessage) {
errorCount ++;
//** Add whatever you like from the error information to this json string.
errorJson = '{"code":"' + error.Status + '", "error":"' + error.Status + '", "details":"' + errorMessage + '"}';
//Appending hidden input with details to document. All console errors can be scraped this way, or just ones that stop page load if you like.
$("body").append("<input type='hidden' class='console-error-saved' id='" + errorCount
+ '"' value='" + errorJson + "'");
}
''')
然后,在您的 Selenium 腳本中,在等待預(yù)期元素出現(xiàn)的同時(shí),如果等待超時(shí)并且仍然找不到該元素,請(qǐng)運(yùn)行以下命令:
pageErrors = driver.execute_script('''
var json = "";
var errors = $('.console-error-saved');
for(var x=0; x < errors.length; x++) {
json += $(errors[x]).text();
if(x < errors.length - 1) {
json += ",";
}
}
return "[" + json + "]";
''')
現(xiàn)在從 Python 解析 json 以從字符串中獲取一個(gè)對(duì)象。查找502、503等特定錯(cuò)誤并報(bào)告,然后調(diào)用刷新命令
import json
errors = json.loads(pageErrors)
#... look at the errors and handle them as needed.
# If qualifying error occurred, refresh the page and do your checks again.
driver.refresh()

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
歡迎來到索。這是方法。
# interval - refresh time
# maxTime - maximum time to wait (rather going into infinite loop)
def refresh_browser_until_element_present(locator_type, locator, interval, maxTime):
startTime = datetime.now()
elements = []
while ((datetime.now() - startTime).seconds<maxTime and len(elements) ==0):
time.sleep(interval)
driver.refresh()
if locator_type == 'xpath':
elements = driver.find_elements_by_xpath(locator)
elif locator_type == 'css':
elements = driver.find_elements_by_css_selector(locator)
使用方法:
refresh_browser_until_element_present('css','#checkCbtaskdiv',15,120)

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
快速查看,根據(jù)此答案,您可以設(shè)置driver.find_element_by_class_name("submit_btn")不.click()帶變量的響應(yīng),然后檢查該變量是否不是None
while elements is None:
elements = driver.find_element_by_class_name("submit_btn")
for e in elements:
e.click()
添加回答
舉報(bào)