2017-07-03 20 views
2

私はPythonとSeleniumを使ってダウンロードを自動化しようとしています。スタートページでは、ポップアップがページに表示されます:PythonでSeleniumを使ってブラウザのポップアップを閉じる方法は?

enter image description here

私はそれはセレンを使用して閉じることができますどのように?

私は、次の方法を試してみましたが、すべて失敗しました:

>>> 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

どのように「を開けないでください」上のダイアログボックスに切り替えて、クリックについて: @Aria-describedby = "dialogContent2"] //ボタン[@ title = "Dont't open"] ')をクリックします。 ) –

+0

アラートが存在することを明示的に待ってみましたか? –

+0

@RayhaneMama明示的な待機とは何ですか?私はそれをどのようにすることができますか? – Abraham

答えて

0

をあなたが最初にそれが待機に関連するエラーだかどうかを確認するために、これを試してみてください。
:ブラウザがアクションを実行する前に警告ダイアログを見つけるのに十分な時間がない場合。明示的な待機についてもっと知ることができます。あなたもexpected_conditionsパッケージを必要とし、以下しようとしますhere

:私は3秒間待つようにprecised

# 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" 

注意を、あなたが好きなようにそれを変更することができます。

このソリューションが動作しない場合は、明示的に名前やテキストなどにより、ダイアログのポップアップを選択するためにトリックを試してみてください。..

関連する問題