Selenium Chromedriver - 点击取消Chrome身份验证弹出窗口

问题描述 投票:0回答:1

我正在为python应用程序使用VPN Chrome应用程序。每当我启动Chrome实例并且Chrome开始在VPN插件准备就绪之前加载网站时,就会显示以下弹出窗口。一旦我点击取消按钮,VPN插件通常就绪,我可以毫无问题地访问互联网。

我正在寻找一种方法来点击Selenium的“取消”按钮。

到目前为止我尝试过的:

  • 将主页设置为Chrome设置 - >弹出窗口没有出现,因为设置不是网站,经过短暂的时间后,我可以在大多数时间没有问题的情况下继续。每隔一段时间,弹出窗口仍会出现。
  • 使用webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()每当我尝试这个时,脚本会冻结,直到我手动点击“取消”按钮,然后执行操作。 driver.get(url)and driver.switch_to.alert()也是如此

谢谢!

Screenshot

编辑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")

如您所见,只要弹出窗口处于打开状态,命令就不会执行:

enter image description here

enter image description here

python python-3.x selenium selenium-webdriver selenium-chromedriver
1个回答
0
投票

1)void dismiss()//单击警报的“取消”按钮。

driver.switchTo().alert().dismiss();

2)void accept()//单击警报的“确定”按钮。

driver.switchTo().alert().accept();

3)String getText()//捕获警报消息。

driver.switchTo().alert().getText();    

4)void sendKeys(String stringToSend)//将一些数据发送到警告框。

driver.switchTo().alert().sendKeys("Text");

你可以参考Guru 99的所有这些

您还应该查看它是哪种警报,浏览器推送通知,浏览器警报等...

这些是我的Chrome选项,用于禁用通知,警报和推送通知

chrome.switches = --incognito;--disable-download-notification;--disable-infobars

另一种实现chrome选项的方法

ChromeOptions ops = new ChromeOptions();
        ops.addArguments("--disable-notifications");
        ops.addArguments("--disable-infobars");
        System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
        driver = new ChromeDriver(ops);

最后,这里是Chrome驱动程序文档,以帮助您进一步。 ChromeDriver Docs

编辑:

driver.switchTo().activeElement();
driver.close()

或者你可以试试

Driver.SwitchTo().frame("NameOfFrame");

Driver.findElement("enter path to cancel button").click();

Driver.SwitchTo().defaultContent();

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.