Selenium failed after waiting

时间:2018-03-23 00:22:03

标签: python multithreading python-3.x selenium python-requests

I'm trying to automatize login to the webpage, which requires PKCS11 security device. Part of the login process is handled by Selenium 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).

First idea was to use python-requests, but I need to use PKCS11 module which is build-in browser-therefore selenium.

Once browser calls PKCS11 module, I'm not able to get any element whatsoever, so I use pyautogui 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.

Here is my 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()

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:

Traceback (most recent call last):
  File "./sign_up.py", line 95, in <module>
    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:

Using different sleeps, error differs just in length of Traceback, but ends with same exception with none text and none message.

I'm using python 3.5, selenium webdriver is Firefox. Any comments would be greatly appreciated!

Edit: I have tried also to get rid of threads and after

browser.find_element_by_id("validate_button").send_keys(Keys.ENTER)

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 browser object fails.

0 个答案:

没有答案