# Buggy code
def login_user(browser, username, password, switch_language=True):
    """Logins the user with the given username and password"""
    browser.get("https://www.instagram.com")

    # Changes instagram language to english, to ensure no errors ensue from having the site on a different language
    # Might cause problems if the OS language is english
    if switch_language:
        browser.find_element_by_xpath(
            "//footer[@class='_oofbn']/div[@class='_mhrsk _pcuq6']/nav["
            "@class='_p1gbi']/ul[@class='_fh0f2']/li[@class='_fw3ds'][10]/"
            "span[@class='_17z9g _c4mil']/select[@class='_nif11']/option"
            "[text()='English']"
        ).click()

    # Check if the first div is 'Create an Account' or 'Log In'
    login_elem = browser.find_element_by_xpath("//article/div/div/p/a[text()='Log in']")
    if login_elem is not None:
        action = ActionChains(browser).move_to_element(login_elem).click().perform()

    # Enter username and password and logs the user in
    # Sometimes the element name isn't 'Username' and 'Password' (valid for placeholder too)
    input_username = browser.find_elements_by_xpath("//input[@name='username']")

    action = (
        ActionChains(browser)
        .move_to_element(input_username[0])
        .click()
        .send_keys(username)
        .perform()
    )
    sleep(1)
    input_password = browser.find_elements_by_xpath("//input[@name='password']")
    ActionChains(browser).move_to_element(input_password[0]).click().send_keys(
        password
    ).perform()

    login_button = browser.find_element_by_xpath("//form/span/button[text()='Log in']")
    action = ActionChains(browser).move_to_element(login_button).click().perform()

    sleep(5)

    # Check if user is logged-in (If there's two 'nav' elements)
    nav = browser.find_elements_by_xpath("//nav")
    if len(nav) == 2:
        return True
    else:
        return False


# Traceback (selenium.common.exceptions.NoSuchElementException)
Traceback (most recent call last):
File "insta_script.py", line 495, in <module>
session.login()
File "/home/pi/Projects/InstaPy/instapy/instapy.py", line 146, in login
if not login_user(self.browser, self.username, self.password, self.switch_language):
File "/home/pi/Projects/InstaPy/instapy/login_util.py", line 13, in login_user
browser.find_element_by_xpath("//footer[@class='_oofbn']/div[@class='_mhrsk _pcuq6']/nav["
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//footer[@class='_oofbn']/div[@class='_mhrsk _pcuq6']/nav[@class='_p1gbi']/ul[@class='_fh0f2']/li[@class='_fw3ds'][10]/span[@class='_17z9g _c4mil']/select[@class='_nif11']/option[text()='English']"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpKCdbVi/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpKCdbVi/extensions/fxdriver@googlecode.com/components/driver-component.js:625)

# Fixed code
def login_user(browser, username, password, switch_language=True):
    """Logins the user with the given username and password"""
    browser.get("https://www.instagram.com")

    # Changes instagram language to english, to ensure no errors ensue from having the site on a different language
    # Might cause problems if the OS language is english
    if switch_language:
        browser.find_element_by_xpath(
            "//footer[@class='_s5vm9']/div[@class='_g7lf5 _9z659']/nav["
            "@class='_luodr']/ul[@class='_g8wl6']/li[@class='_538w0'][10]/"
            "span[@class='_pqycz _hqmnd']/select[@class='_fsoey']/option"
            "[text()='English']"
        ).click()

    # Check if the first div is 'Create an Account' or 'Log In'
    login_elem = browser.find_element_by_xpath("//article/div/div/p/a[text()='Log in']")
    if login_elem is not None:
        action = ActionChains(browser).move_to_element(login_elem).click().perform()

    # Enter username and password and logs the user in
    # Sometimes the element name isn't 'Username' and 'Password' (valid for placeholder too)
    input_username = browser.find_elements_by_xpath("//input[@name='username']")

    action = (
        ActionChains(browser)
        .move_to_element(input_username[0])
        .click()
        .send_keys(username)
        .perform()
    )
    sleep(1)
    input_password = browser.find_elements_by_xpath("//input[@name='password']")
    ActionChains(browser).move_to_element(input_password[0]).click().send_keys(
        password
    ).perform()

    login_button = browser.find_element_by_xpath("//form/span/button[text()='Log in']")
    action = ActionChains(browser).move_to_element(login_button).click().perform()

    sleep(5)

    # Check if user is logged-in (If there's two 'nav' elements)
    nav = browser.find_elements_by_xpath("//nav")
    if len(nav) == 2:
        return True
    else:
        return False