猿问

如何刷新页面直到正确加载

我正在尝试在 python 中使用 selenium web 驱动程序自动化一个网站。由于页面未正确加载的服务器错误而无法获取可点击元素时,它会卡住。我想创建一个函数,如果它没有正确加载(更具体地说,如果它没有获得可点击的元素),它将在 15 秒后自动刷新页面。


from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


driver=webdriver.Chrome(r"C:\Users\Hp\Downloads\chromedriver")


driver.get("https://addguru.in/user/index.php")

driver.maximize_window()


driver.implicitly_wait(15)


username=driver.find_element_by_name("username").send_keys("-------")


password=driver.find_element_by_name("password").send_keys("-------")


driver.implicitly_wait(40)


driver.find_element_by_class_name("submit_btn").click()

""" I want a function here which automatically refresh the page after each 15 sec if  the submit-btn is not clickable (due to propely loading problem) """

browser.find_element_by_id("checkCbtaskdiv").click()



driver.implicitly_wait(10)


炎炎设计
浏览 175回答 3
3回答

温温酱

当这种情况发生时,您至少应该在断言中发布警告。这样你就知道发生了什么错误。如果你这样做了,以下内容会对你有所帮助......在页面初始化时或在它开始加载相关页面时添加它。你也可以在任何页面上这样做,真的。driver.execute_script('''&nbsp; &nbsp; window.errorCount = 0;&nbsp; &nbsp; window.onerror = function (error, url, line, column, errorMessage) {&nbsp; &nbsp; &nbsp; &nbsp; errorCount ++;&nbsp; &nbsp; &nbsp; &nbsp; //** Add whatever you like from the error information to this json string.&nbsp; &nbsp; &nbsp; &nbsp; errorJson = '{"code":"' + error.Status + '", "error":"' + error.Status + '", "details":"' + errorMessage + '"}';&nbsp; &nbsp; &nbsp; &nbsp; //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.&nbsp; &nbsp; &nbsp; &nbsp; $("body").append("<input type='hidden' class='console-error-saved' id='" + errorCount&nbsp;&nbsp; &nbsp; &nbsp;+ '"' value='" + errorJson + "'");&nbsp; &nbsp; }''')然后,在您的 Selenium 脚本中,在等待预期元素出现的同时,如果等待超时并且仍然找不到该元素,请运行以下命令:pageErrors = driver.execute_script('''&nbsp; &nbsp; var json = "";&nbsp;&nbsp; &nbsp; var errors = $('.console-error-saved');&nbsp;&nbsp; &nbsp; for(var x=0; x < errors.length; x++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; json += $(errors[x]).text();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(x < errors.length - 1) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json += ",";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return "[" + json + "]";''')现在从 Python 解析 json 以从字符串中获取一个对象。查找502、503等特定错误并报告,然后调用刷新命令import jsonerrors = 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()

holdtom

欢迎来到索。这是方法。# interval - refresh time# maxTime - maximum time to wait (rather going into infinite loop)def refresh_browser_until_element_present(locator_type, locator, interval, maxTime):&nbsp; &nbsp; startTime = datetime.now()&nbsp; &nbsp; elements = []&nbsp; &nbsp; while ((datetime.now() - startTime).seconds<maxTime and len(elements) ==0):&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(interval)&nbsp; &nbsp; &nbsp; &nbsp; driver.refresh()&nbsp; &nbsp; &nbsp; &nbsp; if locator_type == 'xpath':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elements = driver.find_elements_by_xpath(locator)&nbsp; &nbsp; &nbsp; &nbsp; elif locator_type == 'css':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elements = driver.find_elements_by_css_selector(locator)使用方法:refresh_browser_until_element_present('css','#checkCbtaskdiv',15,120)

ibeautiful

快速查看,根据此答案,您可以设置driver.find_element_by_class_name("submit_btn")不.click()带变量的响应,然后检查该变量是否不是Nonewhile elements is None:&nbsp; &nbsp; elements = driver.find_element_by_class_name("submit_btn")for e in elements:&nbsp; &nbsp; e.click()
随时随地看视频慕课网APP

相关分类

Python
我要回答