运行Python3.7和Selenium:运行脚本时如何回复Firefox密码管理器弹出窗口?

2024-04-20 05:45:32 发布

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

我正在用Selenium学习Python 3.7的第2天

我正在使用WebDriver访问网页。我一直在进步,但现在遇到了困难。虽然我可以在我的普通浏览器(选项/隐私和安全/位置/设置)上轻松禁用Firefox密码管理器弹出窗口,但我的脚本的远程运行(根据定义)浏览器无法识别该配置,并且Firefox弹出窗口会出现

脚本可以忽略弹出窗口并导航目标站点,直到我需要访问的最后一页。此时,该页面的HTML无法访问,直到我手动单击Firefox弹出窗口,将其关闭。一旦我这样做,Firefox web Developer Inspector中该网页的HTML代码就会亮起

现在,HTML代码可能由于其他原因无法访问(就像我说的,学习曲线的第2天),但是Webdriver中是否有一些库或命令允许我自动取消FireFox弹出窗口。它不是任何页面的HTML的一部分,所以我不知所措

编辑:我还应该提到,在我手动关闭FireFox弹出窗口之前,最后一页的大部分内容都是空白的

我添加了以下代码,但仍然得到相同的弹出窗口:

from selenium import webdriver

#Using Firefox to access the Web
options = webdriver.FirefoxOptions()
options.set_preference("dom.webnotifications.enabled", False)
driver = webdriver.Firefox(options=options)
driver.maximize_window()

第二次编辑:这是定义概要文件的当前代码部分,我仍然会得到弹出式密码管理器

import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

#Using Firefox to access the Web
profile = webdriver.FirefoxProfile()

#profile.set_preference("dom.push.enabled", False)
profile.set_preference("dom.webnotifications.enabled", False)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.maximize_window()

Tags: 代码fromimportfalsehtmldriverseleniumenabled