事情起因在于 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/75669.html
標籤:Python
