【发布时间】:2019-04-05 14:22:37
【问题描述】:
我正在为 python 应用程序使用 VPN Chrome 应用程序。每当我启动 Chrome 实例并且 Chrome 在 VPN 插件准备好之前开始加载网站时,就会出现以下弹出窗口。一旦我点击取消按钮,VPN插件通常就准备好了,我可以毫无问题地访问互联网。
我正在寻找一种使用 Selenium 来单击“取消”按钮的方法。
到目前为止我所尝试的:
将主页设置为 Chrome 设置 -> 没有出现弹出窗口,因为设置不是网站,经过很短的时间。睡眠后,我大部分时间都可以毫无问题地继续。每隔一段时间,弹出窗口仍然会出现。
使用
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()每当我尝试此操作时,脚本都会冻结,直到我手动单击“取消”按钮,然后执行该操作。driver.get(url)和driver.switch_to.alert()也是如此
谢谢!
编辑 1(将 Chrome 选项设置为不显示通知和信息栏并不能解决问题):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import selenium.common.exceptions
options = Options()
options.headless = False
options.add_argument("user-data-dir=ChromeProfiles\Profile_{}".format(22))
options.add_argument("--profile-directory=profile_1")
options.add_argument("--disable-notifications")
options.add_argument("--disable-infobars")
driver = webdriver.Chrome(options=options, executable_path="chromedriver.exe")
如您所见,只要弹出窗口打开,命令就不会执行:
【问题讨论】:
-
是否尝试过 chrome 选项来禁用弹出通知?
-
我尝试使用
prefs = {"profile.default_content_setting_values.notifications" : 2}和options.add_experimental_option("prefs",prefs)没有结果。 -
几个月前我在使用 ChromeDriver 时遇到了类似的问题。但是当我使用 driver.switch_to.alert() 将其更改为在 FireFox 浏览器上运行时,它运行良好。你能用
driver.switchTo().alert().dismiss()测试Firefox中是否发生同样的情况吗? -
我刚刚检查了 Firefox + Geckodriver。不幸的是,在加载我的项目所依赖的自定义配置文件时,Firefox 似乎存在一些问题。
-
你有没有找到解决这个问题的方法?
标签: python python-3.x selenium selenium-webdriver selenium-chromedriver