【发布时间】:2022-11-29 02:19:31
【问题描述】:
我在带有 Firefox (107) 驱动程序的 Python (3.11) 中使用 Selenium。
使用驱动程序,我导航到一个页面,在几次操作后,该页面触发操作系统警报(提示我启动程序)。弹出此警报时,驱动程序挂起,只有手动关闭后,我的脚本才会继续运行。
我试过driver.quit(),以及使用
os.system("taskkill /F /pid " + str(process.ProcessId))
与驱动程序的 PID,没有运气。
我已经设法防止弹出窗口弹出
options.set_preference("security.external_protocol_requires_permission", False)
但是代码在弹出窗口的位置仍然以相同的方式挂起将已经弹出。
我不关心程序是否启动,我只需要我的代码在这个关键点不需要人为干预。
这是我目前拥有的一个最小示例:
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.firefox.options import Options
from seleniumwire import webdriver
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options.set_preference("security.external_protocol_requires_permission", False)
driver = webdriver.Firefox(options=options)
# Go to the page
driver.get(url)
user_field = driver.find_element("id", "UserName")
user_field.send_keys(username)
pass_field = driver.find_element("id", "Password")
pass_field.send_keys(password)
pass_field.send_keys(Keys.ENTER)
#this is the point where the pop up appears
reqs = driver.requests
print("Success!")
driver.quit()
【问题讨论】:
-
对我来说,有一个暂停,但代码在几秒钟后继续正常执行。您要消除的是这种延迟吗?还是您的程序会无限期停止?
-
@Lucan Mine 无限期停止。为了保险起见,我什至把它留在了午休时间。
标签: python python-3.x selenium selenium-webdriver selenium-chromedriver