2019年3月21日 星期四

[web, python] 最簡單的方式讓程式自動點選 pop 出來的 alert 視窗


請服用 selenuum

def handle_alert_popup(self):
       # Step 1: wait for alert window  
       # prevent:
       # Message: unexpected alert open: {Alert text :      
       from selenium.webdriver.common.by import By
       from selenium.webdriver.support.ui import WebDriverWait
       from selenium.webdriver.support import expected_conditions as EC

       element = WebDriverWait(self.driver, 10).until(
           EC.alert_is_present()
       )

       # Step 2: click ok
       alert = self.driver.switch_to_alert()

       alert.accept()




Further Reading
  • Python Main Page - Web Automation 章節 (view)