我尝试使用Python和Selenium自动下载。在开始页面中,页面上会出现一个弹出窗口:
如何使用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)
答案 0 :(得分:0)
你应该首先尝试这个,看看是否是与等待相关的错误 即:如果您的浏览器在进行操作之前没有足够的时间找到警报对话框。您可以了解有关显式等待的更多信息。 here
您还需要expected_conditions个包并尝试以下操作:
# add these imports
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
#try to find the alert and do stuff
try:
#wait for the alert to show up
WebDriverWait(browser, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')
#if it does
alert = browser.switch_to.alert()
alert.accept()
print "alert accepted"
except TimeoutException:
print "no alert"
请注意,我准备等待3秒钟,您可以将其更改为您喜欢的任何内容。
如果此解决方案不起作用,您应该尝试通过名称或文本等明确选择对话框弹出的技巧。
答案 1 :(得分:0)
您面临的问题是,弹出窗口不是DOM的一部分,因此硒无法通过发送击键,等待或单击某个地方来处理。弹出窗口是浏览器的本机弹出窗口,因此只能由浏览器本身进行处理,通常是通过用户交互来进行。
为防止交互,您可以预先定义在链接使用某种协议的情况下浏览器应采取的操作。通过电话链接思考hrefs:
<p>Book now, call <a href="tel:01234567890">01234 567 890</a></p>
必须在浏览器启动之前/之前更改用户首选项。协议方案的处理可以在用户首选项中预定义。就我而言,我想拒绝处理方案tel://
。
要在启动时更改用户首选项,请扩展浏览器功能并在prefs
的{{1}}下指定用户首选项:
chromeOptions
对于您来说,它可能是googlePlay链接。因此,请使用相关协议而不是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)
来代替tel: false
。