Selenium在等待后失败

2024-06-17 18:53:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试自动登录到该网页,这需要PKCS11安全设备。登录过程的一部分由Selenium处理,经过一段时间(点击按钮),浏览器通过PKCS11模块(已添加到浏览器的安全设备>新的PKCS11模块)调用读卡器。在

第一个想法是使用python请求,但我需要使用PKCS11模块,它是在浏览器中构建的,因此selenium。在

一旦浏览器调用PKCS11模块,我就无法获得任何元素,所以我使用pyautogui来模拟“人机交互,添加请求的PIN代码并按几个输入键”。之后,我可以在浏览器中看到登录通过了,我想再次使用Selenium(基本上是从中获取cookies并将它们传递给请求,这样我就可以在那里自动执行更多操作)。但是,我不能强迫Selenium等到pyautogui结束。在

这是我的代码:

#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()

我想在child(pyautogui)结束后在我的parents线程(selenium)中等待,然后从parent获取cookies,但我得到了错误。我也尝试过几个等待(因为同步…),隐式或显式,但我仍然得到基本相同的错误:

^{pr2}$

使用不同的休眠,错误只是回溯的长度不同,但以相同的异常结束,即无文本和无消息。在

我使用的是python3.5,selenium webdriver是Firefox。如有任何意见,我们将不胜感激!在

编辑: 我也试着摆脱线程和之后

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

调用了pyautogui函数,但我在获取cookies时也出现了相同的错误。值得注意的是,它不仅仅是获取cookies,对浏览器对象执行的任何操作都会失败。在


Tags: 模块fromimportbrowserbyselenium错误pin