gpt4 book ai didi

python - 如何在 python 中使用 Selenium 关闭浏览器弹出窗口?

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

我正在尝试使用 Python 和 Selenium 自动执行下载。在起始页中,页面出现弹窗:

enter image description here

如何使用 Selenium 关闭它?

我尝试了以下方法,但都失败了:

>>> alert = browser.switch_to_alert()

>>> alert.accept()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/common/alert.py", line 80, in accept
self.driver.execute(Command.ACCEPT_ALERT)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoAlertPresentException: Message: no alert open
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)

>>> alert.dismiss()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/common/alert.py", line 71, in dismiss
self.driver.execute(Command.DISMISS_ALERT)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoAlertPresentException: Message: no alert open
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)


>>>alert = browser.switch_to_window('Open xdg-open?')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 531, in switch_to_window
self._switch_to.window(window_name)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/switch_to.py", line 92, in window
self._driver.execute(Command.SWITCH_TO_WINDOW, data)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchWindowException: Message: no such window
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)


>>> alert = browser.switch_to.window("Open xdg-open?")
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/switch_to.py", line 92, in window
self._driver.execute(Command.SWITCH_TO_WINDOW, data)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchWindowException: Message: no such window
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)

最佳答案

您面临的问题是,弹出窗口不是 DOM 的一部分,因此 selenium 既不能通过发送击键、等待也不能在某处单击来处理。弹出窗口是浏览器 native 弹出窗口,因此只能由浏览器本身处理,通常由用户交互处理。

为防止交互,您可以预先定义在链接使用特定协议(protocol)的情况下浏览器应采取的操作。想想带有电话链接的 hrefs:

<p>Book now, call <a href="tel:01234567890">01234 567 890</a></p>

必须在浏览器启动时/之前更改用户首选项。协议(protocol)方案的处理可以在用户偏好中预定义。在我的例子中,我想拒绝处理 tel:// 方案。

要在启动时更改用户首选项,请扩展浏览器功能并在 chromeOptionsprefs 下指定用户首选项:

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option( "prefs", {'protocol_handler.excluded_schemes.tel': false})
driver = webdriver.Chrome(chrome_options=chrome_options)

在您的情况下,它可能是一个 googlePlay 链接。因此,不要使用 tel: false,而是使用有问题的协议(protocol),而不是 tel

关于python - 如何在 python 中使用 Selenium 关闭浏览器弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44880283/

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