# Buggy code
def browse_website(
    url,
    num_links,
    sleep,
    visit_id,
    webdriver,
    browser_params,
    manager_params,
    extension_socket,
):
    """Calls get_website before visiting <num_links> present on the page.

    Note: the site_url in the site_visits table for the links visited will
    be the site_url of the original page and NOT the url of the links visited.
    """
    # First get the site
    get_website(url, sleep, visit_id, webdriver, browser_params, extension_socket)

    # Connect to logger
    logger = loggingclient(*manager_params["logger_address"])

    # Then visit a few subpages
    for i in range(num_links):
        links = [x for x in get_intra_links(webdriver, url) if x.is_displayed() is True]
        if not links:
            break
        r = int(random.random() * len(links))
        logger.info(
            "BROWSER %i: visiting internal link %s"
            % (browser_params["crawl_id"], links[r].get_attribute("href"))
        )

        try:
            links[r].click()
            wait_until_loaded(webdriver, 300)
            time.sleep(max(1, sleep))
            if browser_params["bot_mitigation"]:
                bot_mitigation(webdriver)
            webdriver.back()
            wait_until_loaded(webdriver, 300)
        except Exception:
            pass


# Traceback (StaleElementReferenceException)
TaskManager          - INFO     -

OpenWPM Version: v0.8.0-137-gb0a8e00
Firefox Version: 52.4.1

========== Browser Configuration ==========
Keys:
{
"crawl_id": 0,
"adblock-plus": 1,
"bot_mitigation": 2,
"browser": 3,
"cookie_instrument": 4,
"cp_instrument": 5,
"disable_flash": 6,
"disconnect": 7,
"donottrack": 8,
"extension_enabled": 9,
"ghostery": 10,
"headless": 11,
"http_instrument": 12,
"https-everywhere": 13,
"js_instrument": 14,
"prefs": 15,
"random_attributes": 16,
"save_all_content": 17,
"save_javascript": 18,
"tp_cookies": 19,
"tracking-protection": 20,
"ublock-origin": 21
}

0  1      2      3        4      5      6      7      8      9     10     11    12    13     14     15    16     17     18     19      20     21
---  -----  -----  -------  -----  -----  -----  -----  -----  ----  -----  ----  ----  -----  -----  ----  -----  -----  -----  ------  -----  -----
19  False  False  firefox  False  False  False  False  False  True  False  True  True  False  False  {}    False  False  False  always  False  False

========== Input profile tar files ==========
No profile tar files specified

========== Output (archive) profile dirs ==========
No profile archive directories specified



BrowserManager       - INFO     - BROWSER 19: EXECUTING COMMAND: ('BROWSE', 'http://www.en.aau.dk/', 50, 0, 16)
Using psl from cache: /tmp/public_suffix_list.dat
browser_commands     - INFO     - BROWSER 19: visiting internal link http://www.en.aau.dk/news/
browser_commands     - INFO     - BROWSER 19: visiting internal link http://www.en.aau.dk/cooperation/
browser_commands     - INFO     - BROWSER 19: visiting internal link http://www.en.aau.dk/events/
browser_commands     - INFO     - BROWSER 19: visiting internal link http://www.vacancies.aau.dk/
BrowserManager       - INFO     - BROWSER 19: Crash in driver, restarting browser manager
Traceback (most recent call last):
File "/user/es.aau.dk/sok/OpenWPM/automation/BrowserManager.py", line 404, in BrowserManager
browser_params, manager_params, extension_socket)
File "/user/es.aau.dk/sok/OpenWPM/automation/Commands/command_executor.py", line 22, in execute_command
extension_socket=extension_socket)
File "/user/es.aau.dk/sok/OpenWPM/automation/Commands/browser_commands.py", line 189, in browse_website
if x.is_displayed() is True]
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 363, in is_displayed
return self._execute(Command.IS_ELEMENT_DISPLAYED)['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: The element reference of <a class="mobileMenu topLink"> stale: either the element is no longer attached to the DOM or the page has been refreshed


TaskManager          - INFO     - BROWSER 19: Received failure status while executing command: BROWSE

# Fixed code
def browse_website(
    url,
    num_links,
    sleep,
    visit_id,
    webdriver,
    browser_params,
    manager_params,
    extension_socket,
):
    """Calls get_website before visiting <num_links> present on the page.

    Note: the site_url in the site_visits table for the links visited will
    be the site_url of the original page and NOT the url of the links visited.
    """
    # First get the site
    get_website(url, sleep, visit_id, webdriver, browser_params, extension_socket)

    # Connect to logger
    logger = loggingclient(*manager_params["logger_address"])

    # Then visit a few subpages
    for _ in range(num_links):
        links = [x for x in get_intra_links(webdriver, url) if is_displayed(x) is True]
        if not links:
            break
        r = int(random.random() * len(links))
        logger.info(
            "BROWSER %i: visiting internal link %s"
            % (browser_params["crawl_id"], links[r].get_attribute("href"))
        )

        try:
            links[r].click()
            wait_until_loaded(webdriver, 300)
            time.sleep(max(1, sleep))
            if browser_params["bot_mitigation"]:
                bot_mitigation(webdriver)
            webdriver.back()
            wait_until_loaded(webdriver, 300)
        except Exception:
            pass