title: Selenium failed after waiting
body: <p>I'm trying to automatize login to the webpage, which requires <strong>PKCS11 security device</strong>. Part of the login process is handled by <strong>Selenium</strong> and after some point (clicking on the button), browser calls the card reader via PKCS11 module (which has been added to the security devices > new PKCS11 Module of the browser).</p>
  
  <p>First idea was to use <strong>python-requests</strong>, but I need to use PKCS11 module which is build-in browser-therefore selenium.</p>
  
  <p>Once browser calls PKCS11 module, I'm not able to get any element whatsoever, so I use <strong>pyautogui</strong> to simulate "human interaction, add requested PIN code and press several Enters". After that, I can see in browser that login passed and I want to work with Selenium again (basically to get cookies from it and pass them to requests, so I can automatize more things there). However, I'm not able to force Selenium to wait until pyautogui ends.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#some procesing made by requests, "s" is our current session
  
  from selenium import webdriver
  from selenium.webdriver.common.keys import Keys
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.wait import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC
  from threading import Thread
  from time import sleep
  import pyautogui
  
  #loading with profile to be able to communicate with PKCS11 module
  profile = webdriver.FirefoxProfile('/home/user/.mozilla/firefox/azo225fg9b.default')
  browser = webdriver.Firefox(profile)
  
  #importing cookies from requests session, we want to keep this session alive
  from requests.utils import dict_from_cookiejar
  cookies = dict_from_cookiejar(s.cookies)
  browser.get("https://sample_url.com/")
  for key, value in cookies.items():
          browser.add_cookie({'name': key, 'value': value})
  
  browser.get("https://sample_url.com/login")
  
  def threaded_write_pin(pin="124567890"):
      sleep(3)
      pyautogui.typewrite(pin, interval=0.25)
      pyautogui.press('enter')
      pyautogui.typewrite("987654321", interval=0.25)
      pyautogui.press('enter')
  
  thread = Thread(target = threaded_write_pin)
  thread.start()
  browser.find_element_by_id("validate_button").send_keys(Keys.ENTER)
  
  #HERE I TRIED DIFFERENT WAITS
  #wait = WebDriverWait(browser, 10)
  #table = wait.until(EC.presence_of_element_located((By.ID, 'secret_tbl1')))
  thread.join()
  
  print (browser.get_cookies())
  
  sys.exit()
  </code></pre>
  
  <p>I want to wait in my parents thread (selenium) once child(pyautogui) ends and then get cookies from parent, but I got error. I have tried also several waits (because of synchronization ...), implicit or explicit, but I'm still getting basically same error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./sign_up.py", line 95, in &lt;module&gt;
      print (browser.get_cookies())
    File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 825, in get_cookies
      return self.execute(Command.GET_ALL_COOKIES)['value']
    File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
      self.error_handler.check_response(response)
    File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 241, in check_response
      raise exception_class(message, screen, stacktrace, alert_text)
  selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
  Message:
  </code></pre>
  
  <p>Using different sleeps, error differs just in length of Traceback, but ends with same exception with none text and none message.</p>
  
  <p>I'm using python 3.5, selenium webdriver is Firefox.  Any comments would be greatly appreciated!</p>
  
  <p>Edit:
  I have tried also to get rid of threads and after</p>
  
  <pre><code>browser.find_element_by_id("validate_button").send_keys(Keys.ENTER)
  </code></pre>
  
  <p>called pyautogui function, but I end up with same error while getting cookies. It is worth noticing that it is not just getting cookies, any action taken on <strong>browser</strong> object fails.</p>