selenium切换到alert弹窗处理

这里提前准备了一段简单的alert页面


  
  
  

下面代码切换处理alert弹窗

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Alert

driver = webdriver.Chrome()
driver.get('file:///Users/cody/Desktop/seleniumAlert.html')

driver.find_element_by_id('only').click()

# 等待弹窗出现
WebDriverWait(driver,20).until(EC.alert_is_present())

# 切换进alert
alert = driver.switch_to.alert
# 打印alert文本内容
print(alert.text)

# 关闭弹框(接受)
alert.accept()
# # 关闭弹窗(拒绝)
# alert.dismiss()
# # 向弹窗里输入内容
# alert.send_keys('selenium')

print(driver.current_url)
driver.quit()

 

你可能感兴趣的:(python自动化,web自动化,selenium,selenium,web自动化)