3 回答
TA贡献1752条经验 获得超4个赞
当这种情况发生时,您至少应该在断言中发布警告。这样你就知道发生了什么错误。如果你这样做了,以下内容会对你有所帮助......
在页面初始化时或在它开始加载相关页面时添加它。你也可以在任何页面上这样做,真的。
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 脚本中,在等待预期元素出现的同时,如果等待超时并且仍然找不到该元素,请运行以下命令:
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 + "]";
''')
现在从 Python 解析 json 以从字符串中获取一个对象。查找502、503等特定错误并报告,然后调用刷新命令
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贡献1805条经验 获得超10个赞
欢迎来到索。这是方法。
# 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贡献1993条经验 获得超5个赞
快速查看,根据此答案,您可以设置driver.find_element_by_class_name("submit_btn")不.click()带变量的响应,然后检查该变量是否不是None
while elements is None:
elements = driver.find_element_by_class_name("submit_btn")
for e in elements:
e.click()
添加回答
举报