Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何刷新页面直到页面正确加载(由于服务器错误,如错误502)_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 如何刷新页面直到页面正确加载(由于服务器错误,如错误502)

Python 如何刷新页面直到页面正确加载(由于服务器错误,如错误502),python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在尝试在python中使用SeleniumWeb驱动程序来自动化一个网站。由于服务器错误(页面未正确加载),无法获取可单击元素时,它会被卡住。我想创建一个函数,如果页面加载不正确(更具体地说,如果没有获得可点击元素),它将在15秒后自动刷新页面 只需快速查看,并根据,您就可以设置驱动程序的响应。通过不带的\u class\u name(“submit\u btn”)查找\u element\u。单击()变量,然后检查该变量是否为None while elements is None:

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


只需快速查看,并根据,您就可以设置
驱动程序的响应。通过不带
的\u class\u name(“submit\u btn”)
查找\u element\u。单击()
变量,然后检查该变量是否为
None

while elements is None:
    elements = driver.find_element_by_class_name("submit_btn")

for e in elements:
    e.click()

只需快速查看,并根据,您就可以设置
驱动程序的响应。通过不带
的\u class\u name(“submit\u btn”)
查找\u element\u。单击()
变量,然后检查该变量是否为
None

while elements is None:
    elements = driver.find_element_by_class_name("submit_btn")

for e in elements:
    e.click()

当这种情况发生时,您至少应该在断言中发布警告。这样您就知道发生了什么错误。如果你这样做,以下内容将帮助你

在页面初始化时或在开始加载相关页面时添加此选项。你也可以在任何页面上这样做,真的

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 + "'");

    }
''')

当这种情况发生时,您至少应该在断言中发布警告。这样您就知道发生了什么错误。如果你这样做,以下内容将帮助你

在页面初始化时或在开始加载相关页面时添加此选项。你也可以在任何页面上这样做,真的

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 + "'");

    }
''')

欢迎来到So。这里是方法

# 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)

欢迎来到So。这里是方法

# 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)