gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:09 26 4
gpt4 key购买 nike

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

我正在寻找一种使用 Selenium 单击“取消”按钮的方法。

到目前为止我已经尝试过:

  • 将主页设置为 Chrome 设置 -> 不会出现弹出窗口,因为设置不是网站,经过短暂的 sleep 后,大多数时候我都可以继续进行,没有任何问题。每隔一段时间,弹出窗口仍然会出现。

  • 使用webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()每当我尝试此操作时,脚本都会卡住,直到我手动单击“取消”按钮,然后执行该操作。 driver.get(url)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

最佳答案

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

希望这有帮助

关于python - Selenium Chromedriver - 单击取消 Chrome 身份验证弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55537531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com