We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Last update ha a bug: local variable 'send_security_code_button' referenced before assignment
@timgrossmann @uluQulu @sionking 

Traceback (most recent call last):
  File "/root/InstaPy/5bcd8f2445908600133a63b6.py", line 18, in <module>
    session.login()
  File "/root/InstaPy/instapy/instapy.py", line 396, in login
    self.bypass_with_mobile):
  File "/root/InstaPy/instapy/login_util.py", line 268, in login_user
    bypass_suspicious_login(browser, bypass_with_mobile)
  File "/root/InstaPy/instapy/login_util.py", line 90, in bypass_suspicious_login
    .move_to_element(send_security_code_button)
UnboundLocalError: local variable 'send_security_code_button' referenced before assignment



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `instapy/login_util.py`
Content:
```
1 """Module only used for the login part of the script"""
2 import time
3 import pickle
4 from selenium.webdriver.common.action_chains import ActionChains
5 
6 from .time_util import sleep
7 from .util import update_activity
8 from .util import web_address_navigator
9 from .util import explicit_wait
10 from .util import click_element
11 
12 from selenium.common.exceptions import NoSuchElementException
13 from selenium.common.exceptions import WebDriverException
14 
15 
16 
17 
18 def bypass_suspicious_login(browser, bypass_with_mobile):
19     """Bypass suspicious loggin attempt verification. This should be only enabled
20     when there isn't available cookie for the username, otherwise it will and
21     shows "Unable to locate email or phone button" message, folollowed by
22     CRITICAL - Wrong login data!"""
23     # close sign up Instagram modal if available
24     try:
25         close_button = browser.find_element_by_xpath("[text()='Close']")
26 
27         (ActionChains(browser)
28             .move_to_element(close_button)
29             .click()
30             .perform())
31 
32         # update server calls
33         update_activity()
34 
35     except NoSuchElementException:
36         pass
37 
38     try:
39         # click on "This was me" button if challenge page was called
40         this_was_me_button = browser.find_element_by_xpath(
41             "//button[@name='choice'][text()='This Was Me']")
42 
43         (ActionChains(browser)
44             .move_to_element(this_was_me_button)
45             .click()
46             .perform())
47 
48         # update server calls
49         update_activity()
50 
51     except NoSuchElementException:
52         # no verification needed
53         pass
54 
55     try:
56         choice = browser.find_element_by_xpath(
57             "//label[@for='choice_1']").text
58 
59     except NoSuchElementException:
60         try:
61             choice = browser.find_element_by_xpath(
62                 "//label[@class='_q0nt5']").text
63 
64         except:
65             try:
66                 choice = browser.find_element_by_xpath(
67                     "//label[@class='_q0nt5 _a7z3k']").text
68 
69             except:
70                 print("Unable to locate email or phone button, maybe "
71                         "bypass_suspicious_login=True isn't needed anymore.")
72                 return False
73 
74     if bypass_with_mobile:
75         choice = browser.find_element_by_xpath(
76             "//label[@for='choice_0']").text
77 
78         send_security_code_button = browser.find_element_by_xpath(
79             "//button[text()='Send Security Code']")
80 
81         mobile_button = browser.find_element_by_xpath(
82             "//label[@for='choice_0']")
83 
84         (ActionChains(browser)
85             .move_to_element(mobile_button)
86             .click()
87             .perform())
88         
89         sleep(5)
90 
91     (ActionChains(browser)
92         .move_to_element(send_security_code_button)
93         .click()
94         .perform())
95 
96     # update server calls
97     update_activity()
98 
99     print('Instagram detected an unusual login attempt')
100     print('A security code was sent to your {}'.format(choice))
101     security_code = input('Type the security code here: ')
102 
103     security_code_field = browser.find_element_by_xpath((
104         "//input[@id='security_code']"))
105 
106     (ActionChains(browser)
107         .move_to_element(security_code_field)
108         .click()
109         .send_keys(security_code)
110         .perform())
111 
112     # update server calls for both 'click' and 'send_keys' actions
113     for i in range(2):
114         update_activity()
115 
116     submit_security_code_button = browser.find_element_by_xpath(
117                                             "//button[text()='Submit']")
118 
119     (ActionChains(browser)
120         .move_to_element(submit_security_code_button)
121         .click()
122         .perform())
123 
124     # update server calls
125     update_activity()
126 
127     try:
128         sleep(5)
129         # locate wrong security code message
130         wrong_login = browser.find_element_by_xpath((
131             "//p[text()='Please check the code we sent you and try "
132             "again.']"))
133 
134         if wrong_login is not None:
135             print(('Wrong security code! Please check the code Instagram'
136                    'sent you and try again.'))
137 
138     except NoSuchElementException:
139         # correct security code
140         pass
141 
142 
143 
144 def login_user(browser,
145                username,
146                password,
147                logger,
148                logfolder,
149                switch_language=True,
150                bypass_suspicious_attempt=False,
151                bypass_with_mobile=False
152                ):
153     """Logins the user with the given username and password"""
154     assert username, 'Username not provided'
155     assert password, 'Password not provided'
156 
157     ig_homepage = "https://www.instagram.com"
158     web_address_navigator(browser, ig_homepage)
159     cookie_loaded = False
160 
161     # try to load cookie from username
162     try:
163         googledotcom = "https://www.google.com"
164         web_address_navigator(browser, googledotcom)
165         for cookie in pickle.load(open('{0}{1}_cookie.pkl'
166                                        .format(logfolder,username), 'rb')):
167             browser.add_cookie(cookie)
168             cookie_loaded = True
169     except (WebDriverException, OSError, IOError):
170         print("Cookie file not found, creating cookie...")
171 
172     # include time.sleep(1) to prevent getting stuck on google.com
173     time.sleep(1)
174     
175     web_address_navigator(browser, ig_homepage)
176 
177     # Cookie has been loaded, user should be logged in. Ensurue this is true
178     login_elem = browser.find_elements_by_xpath(
179         "//*[contains(text(), 'Log in')]")
180     # Login text is not found, user logged in
181     # If not, issue with cookie, create new cookie
182     if len(login_elem) == 0:
183         dismiss_notification_offer(browser, logger)
184         return True
185 
186     # If not, issue with cookie, create new cookie
187     if cookie_loaded:
188         print("Issue with cookie for user " + username
189               + ". Creating new cookie...")
190 
191     # Changes instagram language to english, to ensure no errors ensue from
192     # having the site on a different language
193     # Might cause problems if the OS language is english
194     if switch_language:
195         language_element_ENG = browser.find_element_by_xpath(
196           "//select[@class='hztqj']/option[text()='English']")
197         click_element(browser, language_element_ENG)
198 
199     # Check if the first div is 'Create an Account' or 'Log In'
200     login_elem = browser.find_element_by_xpath(
201         "//article//a[text()='Log in']")
202 
203     if login_elem is not None:
204         (ActionChains(browser)
205             .move_to_element(login_elem)
206             .click()
207             .perform())
208 
209         # update server calls
210         update_activity()
211 
212     # Enter username and password and logs the user in
213     # Sometimes the element name isn't 'Username' and 'Password'
214     # (valid for placeholder too)
215 
216     # wait until it navigates to the login page
217     login_page_title = "Login"
218     explicit_wait(browser, "TC", login_page_title, logger)
219 
220     # wait until the 'username' input element is located and visible
221     input_username_XP = "//input[@name='username']"
222     explicit_wait(browser, "VOEL", [input_username_XP, "XPath"], logger)
223 
224     input_username = browser.find_element_by_xpath(input_username_XP)
225 
226     (ActionChains(browser)
227         .move_to_element(input_username)
228         .click()
229         .send_keys(username)
230         .perform())
231 
232     # update server calls for both 'click' and 'send_keys' actions
233     for i in range(2):
234         update_activity()
235 
236     sleep(1)
237 
238     #  password
239     input_password = browser.find_elements_by_xpath(
240         "//input[@name='password']")
241 
242     if not isinstance(password, str):
243         password = str(password)
244 
245     (ActionChains(browser)
246         .move_to_element(input_password[0])
247         .click()
248         .send_keys(password)
249         .perform())
250 
251     # update server calls for both 'click' and 'send_keys' actions
252     for i in range(2):
253         update_activity()
254 
255     login_button = browser.find_element_by_xpath(
256         "//button[text()='Log in']")
257 
258     (ActionChains(browser)
259         .move_to_element(login_button)
260         .click()
261         .perform())
262 
263     # update server calls
264     update_activity()
265 
266     dismiss_get_app_offer(browser, logger)
267     dismiss_notification_offer(browser, logger)
268 
269     if bypass_suspicious_attempt is True:
270         bypass_suspicious_login(browser, bypass_with_mobile)
271 
272     sleep(5)
273 
274     # Check if user is logged-in (If there's two 'nav' elements)
275     nav = browser.find_elements_by_xpath('//nav')
276     if len(nav) == 2:
277         # create cookie for username
278         pickle.dump(browser.get_cookies(),
279                     open('{0}{1}_cookie.pkl'.format(logfolder,username), 'wb'))
280         return True
281     else:
282         return False
283 
284 
285 
286 def dismiss_get_app_offer(browser, logger):
287     """ Dismiss 'Get the Instagram App' page after a fresh login """
288     offer_elem = "//*[contains(text(), 'Get App')]"
289     dismiss_elem = "//*[contains(text(), 'Not Now')]"
290 
291     # wait a bit and see if the 'Get App' offer rises up
292     offer_loaded = explicit_wait(browser, "VOEL", [offer_elem, "XPath"], logger, 5, False)
293 
294     if offer_loaded:
295         dismiss_elem = browser.find_element_by_xpath(dismiss_elem)
296         click_element(browser, dismiss_elem)
297 
298 
299 
300 def dismiss_notification_offer(browser, logger):
301     """ Dismiss 'Turn on Notifications' offer on session start """
302     offer_elem_loc = "//div/h2[text()='Turn on Notifications']"
303     dismiss_elem_loc = "//button[text()='Not Now']"
304 
305     # wait a bit and see if the 'Turn on Notifications' offer rises up
306     offer_loaded = explicit_wait(browser, "VOEL", [offer_elem_loc, "XPath"], logger, 4, False)
307 
308     if offer_loaded:
309         dismiss_elem = browser.find_element_by_xpath(dismiss_elem_loc)
310         click_element(browser, dismiss_elem)
311 
312 
313 
314
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/instapy/login_util.py b/instapy/login_util.py
--- a/instapy/login_util.py
+++ b/instapy/login_util.py
@@ -75,9 +75,6 @@
         choice = browser.find_element_by_xpath(
             "//label[@for='choice_0']").text
 
-        send_security_code_button = browser.find_element_by_xpath(
-            "//button[text()='Send Security Code']")
-
         mobile_button = browser.find_element_by_xpath(
             "//label[@for='choice_0']")
 
@@ -88,6 +85,9 @@
         
         sleep(5)
 
+    send_security_code_button = browser.find_element_by_xpath(
+            "//button[text()='Send Security Code']")
+
     (ActionChains(browser)
         .move_to_element(send_security_code_button)
         .click()

{"golden_diff": "diff --git a/instapy/login_util.py b/instapy/login_util.py\n--- a/instapy/login_util.py\n+++ b/instapy/login_util.py\n@@ -75,9 +75,6 @@\n         choice = browser.find_element_by_xpath(\n             \"//label[@for='choice_0']\").text\n \n-        send_security_code_button = browser.find_element_by_xpath(\n-            \"//button[text()='Send Security Code']\")\n-\n         mobile_button = browser.find_element_by_xpath(\n             \"//label[@for='choice_0']\")\n \n@@ -88,6 +85,9 @@\n         \n         sleep(5)\n \n+    send_security_code_button = browser.find_element_by_xpath(\n+            \"//button[text()='Send Security Code']\")\n+\n     (ActionChains(browser)\n         .move_to_element(send_security_code_button)\n         .click()\n", "issue": "Last update ha a bug: local variable 'send_security_code_button' referenced before assignment\n@timgrossmann @uluQulu @sionking \r\n\r\nTraceback (most recent call last):\r\n  File \"/root/InstaPy/5bcd8f2445908600133a63b6.py\", line 18, in <module>\r\n    session.login()\r\n  File \"/root/InstaPy/instapy/instapy.py\", line 396, in login\r\n    self.bypass_with_mobile):\r\n  File \"/root/InstaPy/instapy/login_util.py\", line 268, in login_user\r\n    bypass_suspicious_login(browser, bypass_with_mobile)\r\n  File \"/root/InstaPy/instapy/login_util.py\", line 90, in bypass_suspicious_login\r\n    .move_to_element(send_security_code_button)\r\nUnboundLocalError: local variable 'send_security_code_button' referenced before assignment\r\n\r\n\n", "before_files": [{"content": "\"\"\"Module only used for the login part of the script\"\"\"\nimport time\nimport pickle\nfrom selenium.webdriver.common.action_chains import ActionChains\n\nfrom .time_util import sleep\nfrom .util import update_activity\nfrom .util import web_address_navigator\nfrom .util import explicit_wait\nfrom .util import click_element\n\nfrom selenium.common.exceptions import NoSuchElementException\nfrom selenium.common.exceptions import WebDriverException\n\n\n\n\ndef bypass_suspicious_login(browser, bypass_with_mobile):\n    \"\"\"Bypass suspicious loggin attempt verification. This should be only enabled\n    when there isn't available cookie for the username, otherwise it will and\n    shows \"Unable to locate email or phone button\" message, folollowed by\n    CRITICAL - Wrong login data!\"\"\"\n    # close sign up Instagram modal if available\n    try:\n        close_button = browser.find_element_by_xpath(\"[text()='Close']\")\n\n        (ActionChains(browser)\n            .move_to_element(close_button)\n            .click()\n            .perform())\n\n        # update server calls\n        update_activity()\n\n    except NoSuchElementException:\n        pass\n\n    try:\n        # click on \"This was me\" button if challenge page was called\n        this_was_me_button = browser.find_element_by_xpath(\n            \"//button[@name='choice'][text()='This Was Me']\")\n\n        (ActionChains(browser)\n            .move_to_element(this_was_me_button)\n            .click()\n            .perform())\n\n        # update server calls\n        update_activity()\n\n    except NoSuchElementException:\n        # no verification needed\n        pass\n\n    try:\n        choice = browser.find_element_by_xpath(\n            \"//label[@for='choice_1']\").text\n\n    except NoSuchElementException:\n        try:\n            choice = browser.find_element_by_xpath(\n                \"//label[@class='_q0nt5']\").text\n\n        except:\n            try:\n                choice = browser.find_element_by_xpath(\n                    \"//label[@class='_q0nt5 _a7z3k']\").text\n\n            except:\n                print(\"Unable to locate email or phone button, maybe \"\n                        \"bypass_suspicious_login=True isn't needed anymore.\")\n                return False\n\n    if bypass_with_mobile:\n        choice = browser.find_element_by_xpath(\n            \"//label[@for='choice_0']\").text\n\n        send_security_code_button = browser.find_element_by_xpath(\n            \"//button[text()='Send Security Code']\")\n\n        mobile_button = browser.find_element_by_xpath(\n            \"//label[@for='choice_0']\")\n\n        (ActionChains(browser)\n            .move_to_element(mobile_button)\n            .click()\n            .perform())\n        \n        sleep(5)\n\n    (ActionChains(browser)\n        .move_to_element(send_security_code_button)\n        .click()\n        .perform())\n\n    # update server calls\n    update_activity()\n\n    print('Instagram detected an unusual login attempt')\n    print('A security code was sent to your {}'.format(choice))\n    security_code = input('Type the security code here: ')\n\n    security_code_field = browser.find_element_by_xpath((\n        \"//input[@id='security_code']\"))\n\n    (ActionChains(browser)\n        .move_to_element(security_code_field)\n        .click()\n        .send_keys(security_code)\n        .perform())\n\n    # update server calls for both 'click' and 'send_keys' actions\n    for i in range(2):\n        update_activity()\n\n    submit_security_code_button = browser.find_element_by_xpath(\n                                            \"//button[text()='Submit']\")\n\n    (ActionChains(browser)\n        .move_to_element(submit_security_code_button)\n        .click()\n        .perform())\n\n    # update server calls\n    update_activity()\n\n    try:\n        sleep(5)\n        # locate wrong security code message\n        wrong_login = browser.find_element_by_xpath((\n            \"//p[text()='Please check the code we sent you and try \"\n            \"again.']\"))\n\n        if wrong_login is not None:\n            print(('Wrong security code! Please check the code Instagram'\n                   'sent you and try again.'))\n\n    except NoSuchElementException:\n        # correct security code\n        pass\n\n\n\ndef login_user(browser,\n               username,\n               password,\n               logger,\n               logfolder,\n               switch_language=True,\n               bypass_suspicious_attempt=False,\n               bypass_with_mobile=False\n               ):\n    \"\"\"Logins the user with the given username and password\"\"\"\n    assert username, 'Username not provided'\n    assert password, 'Password not provided'\n\n    ig_homepage = \"https://www.instagram.com\"\n    web_address_navigator(browser, ig_homepage)\n    cookie_loaded = False\n\n    # try to load cookie from username\n    try:\n        googledotcom = \"https://www.google.com\"\n        web_address_navigator(browser, googledotcom)\n        for cookie in pickle.load(open('{0}{1}_cookie.pkl'\n                                       .format(logfolder,username), 'rb')):\n            browser.add_cookie(cookie)\n            cookie_loaded = True\n    except (WebDriverException, OSError, IOError):\n        print(\"Cookie file not found, creating cookie...\")\n\n    # include time.sleep(1) to prevent getting stuck on google.com\n    time.sleep(1)\n    \n    web_address_navigator(browser, ig_homepage)\n\n    # Cookie has been loaded, user should be logged in. Ensurue this is true\n    login_elem = browser.find_elements_by_xpath(\n        \"//*[contains(text(), 'Log in')]\")\n    # Login text is not found, user logged in\n    # If not, issue with cookie, create new cookie\n    if len(login_elem) == 0:\n        dismiss_notification_offer(browser, logger)\n        return True\n\n    # If not, issue with cookie, create new cookie\n    if cookie_loaded:\n        print(\"Issue with cookie for user \" + username\n              + \". Creating new cookie...\")\n\n    # Changes instagram language to english, to ensure no errors ensue from\n    # having the site on a different language\n    # Might cause problems if the OS language is english\n    if switch_language:\n        language_element_ENG = browser.find_element_by_xpath(\n          \"//select[@class='hztqj']/option[text()='English']\")\n        click_element(browser, language_element_ENG)\n\n    # Check if the first div is 'Create an Account' or 'Log In'\n    login_elem = browser.find_element_by_xpath(\n        \"//article//a[text()='Log in']\")\n\n    if login_elem is not None:\n        (ActionChains(browser)\n            .move_to_element(login_elem)\n            .click()\n            .perform())\n\n        # update server calls\n        update_activity()\n\n    # Enter username and password and logs the user in\n    # Sometimes the element name isn't 'Username' and 'Password'\n    # (valid for placeholder too)\n\n    # wait until it navigates to the login page\n    login_page_title = \"Login\"\n    explicit_wait(browser, \"TC\", login_page_title, logger)\n\n    # wait until the 'username' input element is located and visible\n    input_username_XP = \"//input[@name='username']\"\n    explicit_wait(browser, \"VOEL\", [input_username_XP, \"XPath\"], logger)\n\n    input_username = browser.find_element_by_xpath(input_username_XP)\n\n    (ActionChains(browser)\n        .move_to_element(input_username)\n        .click()\n        .send_keys(username)\n        .perform())\n\n    # update server calls for both 'click' and 'send_keys' actions\n    for i in range(2):\n        update_activity()\n\n    sleep(1)\n\n    #  password\n    input_password = browser.find_elements_by_xpath(\n        \"//input[@name='password']\")\n\n    if not isinstance(password, str):\n        password = str(password)\n\n    (ActionChains(browser)\n        .move_to_element(input_password[0])\n        .click()\n        .send_keys(password)\n        .perform())\n\n    # update server calls for both 'click' and 'send_keys' actions\n    for i in range(2):\n        update_activity()\n\n    login_button = browser.find_element_by_xpath(\n        \"//button[text()='Log in']\")\n\n    (ActionChains(browser)\n        .move_to_element(login_button)\n        .click()\n        .perform())\n\n    # update server calls\n    update_activity()\n\n    dismiss_get_app_offer(browser, logger)\n    dismiss_notification_offer(browser, logger)\n\n    if bypass_suspicious_attempt is True:\n        bypass_suspicious_login(browser, bypass_with_mobile)\n\n    sleep(5)\n\n    # Check if user is logged-in (If there's two 'nav' elements)\n    nav = browser.find_elements_by_xpath('//nav')\n    if len(nav) == 2:\n        # create cookie for username\n        pickle.dump(browser.get_cookies(),\n                    open('{0}{1}_cookie.pkl'.format(logfolder,username), 'wb'))\n        return True\n    else:\n        return False\n\n\n\ndef dismiss_get_app_offer(browser, logger):\n    \"\"\" Dismiss 'Get the Instagram App' page after a fresh login \"\"\"\n    offer_elem = \"//*[contains(text(), 'Get App')]\"\n    dismiss_elem = \"//*[contains(text(), 'Not Now')]\"\n\n    # wait a bit and see if the 'Get App' offer rises up\n    offer_loaded = explicit_wait(browser, \"VOEL\", [offer_elem, \"XPath\"], logger, 5, False)\n\n    if offer_loaded:\n        dismiss_elem = browser.find_element_by_xpath(dismiss_elem)\n        click_element(browser, dismiss_elem)\n\n\n\ndef dismiss_notification_offer(browser, logger):\n    \"\"\" Dismiss 'Turn on Notifications' offer on session start \"\"\"\n    offer_elem_loc = \"//div/h2[text()='Turn on Notifications']\"\n    dismiss_elem_loc = \"//button[text()='Not Now']\"\n\n    # wait a bit and see if the 'Turn on Notifications' offer rises up\n    offer_loaded = explicit_wait(browser, \"VOEL\", [offer_elem_loc, \"XPath\"], logger, 4, False)\n\n    if offer_loaded:\n        dismiss_elem = browser.find_element_by_xpath(dismiss_elem_loc)\n        click_element(browser, dismiss_elem)\n\n\n\n", "path": "instapy/login_util.py"}], "after_files": [{"content": "\"\"\"Module only used for the login part of the script\"\"\"\nimport time\nimport pickle\nfrom selenium.webdriver.common.action_chains import ActionChains\n\nfrom .time_util import sleep\nfrom .util import update_activity\nfrom .util import web_address_navigator\nfrom .util import explicit_wait\nfrom .util import click_element\n\nfrom selenium.common.exceptions import NoSuchElementException\nfrom selenium.common.exceptions import WebDriverException\n\n\n\n\ndef bypass_suspicious_login(browser, bypass_with_mobile):\n    \"\"\"Bypass suspicious loggin attempt verification. This should be only enabled\n    when there isn't available cookie for the username, otherwise it will and\n    shows \"Unable to locate email or phone button\" message, folollowed by\n    CRITICAL - Wrong login data!\"\"\"\n    # close sign up Instagram modal if available\n    try:\n        close_button = browser.find_element_by_xpath(\"[text()='Close']\")\n\n        (ActionChains(browser)\n            .move_to_element(close_button)\n            .click()\n            .perform())\n\n        # update server calls\n        update_activity()\n\n    except NoSuchElementException:\n        pass\n\n    try:\n        # click on \"This was me\" button if challenge page was called\n        this_was_me_button = browser.find_element_by_xpath(\n            \"//button[@name='choice'][text()='This Was Me']\")\n\n        (ActionChains(browser)\n            .move_to_element(this_was_me_button)\n            .click()\n            .perform())\n\n        # update server calls\n        update_activity()\n\n    except NoSuchElementException:\n        # no verification needed\n        pass\n\n    try:\n        choice = browser.find_element_by_xpath(\n            \"//label[@for='choice_1']\").text\n\n    except NoSuchElementException:\n        try:\n            choice = browser.find_element_by_xpath(\n                \"//label[@class='_q0nt5']\").text\n\n        except:\n            try:\n                choice = browser.find_element_by_xpath(\n                    \"//label[@class='_q0nt5 _a7z3k']\").text\n\n            except:\n                print(\"Unable to locate email or phone button, maybe \"\n                        \"bypass_suspicious_login=True isn't needed anymore.\")\n                return False\n\n    if bypass_with_mobile:\n        choice = browser.find_element_by_xpath(\n            \"//label[@for='choice_0']\").text\n\n        mobile_button = browser.find_element_by_xpath(\n            \"//label[@for='choice_0']\")\n\n        (ActionChains(browser)\n            .move_to_element(mobile_button)\n            .click()\n            .perform())\n        \n        sleep(5)\n\n    send_security_code_button = browser.find_element_by_xpath(\n            \"//button[text()='Send Security Code']\")\n\n    (ActionChains(browser)\n        .move_to_element(send_security_code_button)\n        .click()\n        .perform())\n\n    # update server calls\n    update_activity()\n\n    print('Instagram detected an unusual login attempt')\n    print('A security code was sent to your {}'.format(choice))\n    security_code = input('Type the security code here: ')\n\n    security_code_field = browser.find_element_by_xpath((\n        \"//input[@id='security_code']\"))\n\n    (ActionChains(browser)\n        .move_to_element(security_code_field)\n        .click()\n        .send_keys(security_code)\n        .perform())\n\n    # update server calls for both 'click' and 'send_keys' actions\n    for i in range(2):\n        update_activity()\n\n    submit_security_code_button = browser.find_element_by_xpath(\n                                            \"//button[text()='Submit']\")\n\n    (ActionChains(browser)\n        .move_to_element(submit_security_code_button)\n        .click()\n        .perform())\n\n    # update server calls\n    update_activity()\n\n    try:\n        sleep(5)\n        # locate wrong security code message\n        wrong_login = browser.find_element_by_xpath((\n            \"//p[text()='Please check the code we sent you and try \"\n            \"again.']\"))\n\n        if wrong_login is not None:\n            print(('Wrong security code! Please check the code Instagram'\n                   'sent you and try again.'))\n\n    except NoSuchElementException:\n        # correct security code\n        pass\n\n\n\ndef login_user(browser,\n               username,\n               password,\n               logger,\n               logfolder,\n               switch_language=True,\n               bypass_suspicious_attempt=False,\n               bypass_with_mobile=False\n               ):\n    \"\"\"Logins the user with the given username and password\"\"\"\n    assert username, 'Username not provided'\n    assert password, 'Password not provided'\n\n    ig_homepage = \"https://www.instagram.com\"\n    web_address_navigator(browser, ig_homepage)\n    cookie_loaded = False\n\n    # try to load cookie from username\n    try:\n        googledotcom = \"https://www.google.com\"\n        web_address_navigator(browser, googledotcom)\n        for cookie in pickle.load(open('{0}{1}_cookie.pkl'\n                                       .format(logfolder,username), 'rb')):\n            browser.add_cookie(cookie)\n            cookie_loaded = True\n    except (WebDriverException, OSError, IOError):\n        print(\"Cookie file not found, creating cookie...\")\n\n    # include time.sleep(1) to prevent getting stuck on google.com\n    time.sleep(1)\n    \n    web_address_navigator(browser, ig_homepage)\n\n    # Cookie has been loaded, user should be logged in. Ensurue this is true\n    login_elem = browser.find_elements_by_xpath(\n        \"//*[contains(text(), 'Log in')]\")\n    # Login text is not found, user logged in\n    # If not, issue with cookie, create new cookie\n    if len(login_elem) == 0:\n        dismiss_notification_offer(browser, logger)\n        return True\n\n    # If not, issue with cookie, create new cookie\n    if cookie_loaded:\n        print(\"Issue with cookie for user \" + username\n              + \". Creating new cookie...\")\n\n    # Changes instagram language to english, to ensure no errors ensue from\n    # having the site on a different language\n    # Might cause problems if the OS language is english\n    if switch_language:\n        language_element_ENG = browser.find_element_by_xpath(\n          \"//select[@class='hztqj']/option[text()='English']\")\n        click_element(browser, language_element_ENG)\n\n    # Check if the first div is 'Create an Account' or 'Log In'\n    login_elem = browser.find_element_by_xpath(\n        \"//article//a[text()='Log in']\")\n\n    if login_elem is not None:\n        (ActionChains(browser)\n            .move_to_element(login_elem)\n            .click()\n            .perform())\n\n        # update server calls\n        update_activity()\n\n    # Enter username and password and logs the user in\n    # Sometimes the element name isn't 'Username' and 'Password'\n    # (valid for placeholder too)\n\n    # wait until it navigates to the login page\n    login_page_title = \"Login\"\n    explicit_wait(browser, \"TC\", login_page_title, logger)\n\n    # wait until the 'username' input element is located and visible\n    input_username_XP = \"//input[@name='username']\"\n    explicit_wait(browser, \"VOEL\", [input_username_XP, \"XPath\"], logger)\n\n    input_username = browser.find_element_by_xpath(input_username_XP)\n\n    (ActionChains(browser)\n        .move_to_element(input_username)\n        .click()\n        .send_keys(username)\n        .perform())\n\n    # update server calls for both 'click' and 'send_keys' actions\n    for i in range(2):\n        update_activity()\n\n    sleep(1)\n\n    #  password\n    input_password = browser.find_elements_by_xpath(\n        \"//input[@name='password']\")\n\n    if not isinstance(password, str):\n        password = str(password)\n\n    (ActionChains(browser)\n        .move_to_element(input_password[0])\n        .click()\n        .send_keys(password)\n        .perform())\n\n    # update server calls for both 'click' and 'send_keys' actions\n    for i in range(2):\n        update_activity()\n\n    login_button = browser.find_element_by_xpath(\n        \"//button[text()='Log in']\")\n\n    (ActionChains(browser)\n        .move_to_element(login_button)\n        .click()\n        .perform())\n\n    # update server calls\n    update_activity()\n\n    dismiss_get_app_offer(browser, logger)\n    dismiss_notification_offer(browser, logger)\n\n    if bypass_suspicious_attempt is True:\n        bypass_suspicious_login(browser, bypass_with_mobile)\n\n    sleep(5)\n\n    # Check if user is logged-in (If there's two 'nav' elements)\n    nav = browser.find_elements_by_xpath('//nav')\n    if len(nav) == 2:\n        # create cookie for username\n        pickle.dump(browser.get_cookies(),\n                    open('{0}{1}_cookie.pkl'.format(logfolder,username), 'wb'))\n        return True\n    else:\n        return False\n\n\n\ndef dismiss_get_app_offer(browser, logger):\n    \"\"\" Dismiss 'Get the Instagram App' page after a fresh login \"\"\"\n    offer_elem = \"//*[contains(text(), 'Get App')]\"\n    dismiss_elem = \"//*[contains(text(), 'Not Now')]\"\n\n    # wait a bit and see if the 'Get App' offer rises up\n    offer_loaded = explicit_wait(browser, \"VOEL\", [offer_elem, \"XPath\"], logger, 5, False)\n\n    if offer_loaded:\n        dismiss_elem = browser.find_element_by_xpath(dismiss_elem)\n        click_element(browser, dismiss_elem)\n\n\n\ndef dismiss_notification_offer(browser, logger):\n    \"\"\" Dismiss 'Turn on Notifications' offer on session start \"\"\"\n    offer_elem_loc = \"//div/h2[text()='Turn on Notifications']\"\n    dismiss_elem_loc = \"//button[text()='Not Now']\"\n\n    # wait a bit and see if the 'Turn on Notifications' offer rises up\n    offer_loaded = explicit_wait(browser, \"VOEL\", [offer_elem_loc, \"XPath\"], logger, 4, False)\n\n    if offer_loaded:\n        dismiss_elem = browser.find_element_by_xpath(dismiss_elem_loc)\n        click_element(browser, dismiss_elem)\n\n\n\n", "path": "instapy/login_util.py"}]}