Selenium中Chrome驱动无法获取alert
事情起因在于 testerhome的一位楼主发布了一个帖子 https://testerhome.com/topics/24900
有一个链接 http://www.yizhike.com.cn/chou/a.html
A页面打开有个按钮,点击后会弹出B新窗口,B窗口只有一个js代码,会弹出alert
楼主求助于无法获取alert中的值
于是我也来试试
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
先上代码
from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC s = webdriver.Chrome() s.get('http://www.yizhike.com.cn/chou/a.html') s.implicitly_wait(10) nowhande = s.window_handles[0] s.find_element_by_id('submit').click() s.implicitly_wait(10) handles = s.window_handles for hand in handles: if nowhande != hand: s.switch_to.window(hand) break else: s.close() # alert = WebDriverWait(s, 5, 0.1).until(EC.alert_is_present()) time.sleep(2) alert=s.switch_to.alert print(alert.text) time.sleep(2) alert.accept() s.quit()
思路大致是,打开A链接——点击按钮——切换到b窗口,关闭A窗口———切换到alert——获取alert的值
然而昨晚试了半天,也没能成功用Chrome驱动获取到alert 一直提示 no such alert
早上起来改动了代码,就是上面贴的。结果导致 程序运行到
alert=s.switch_to.alert
这一行的时候,程序没有任何响应,也不报错
使用显示等待结果也是一样。
之后换Firefox驱动也不行,最后换成IE驱动就可以获取了。但是不知道什么原因。
s = webdriver.Ie()

浙公网安备 33010602011771号