python+selenium:判斷彈出框是否出現,如果彈出框出現,則點擊關閉按鈕,如果沒有彈框,就繼續下一步的操作



self.driver.find_element(By.CSS_SELECTOR,"xxx").click()
first_handle = self.driver.current_window_handle

#如果出現彈框
try:
  first_handle = self.driver.current_window_handle
  time.sleep(1)
  handles = self.driver.window_handles
  time.sleep(3)
  for i in handles:
    if i != first_handle:
      self.driver.close() # 關閉當前窗口
  self.driver.switch_to.window(i)
  self.driver.find_element_by_class_name("xxx").click()
  time.sleep(3)

except:

  #如果沒有出現彈框
  time.sleep(2)

#有彈框關閉之后/或者無彈框之后的操作:也算是公共操作吧

self.driver.find_element...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM