python+selenium 使用switch_to_alert 出现的怪异常
来源:互联网 发布:mysql中的level 编辑:程序博客网 时间:2024/06/16 21:16
'''
Created on 2014年11月22日
@author :songjin
'''
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import firefox
from selenium.webdriver.common.keys import Keys
#firefoxdriverpath=os.path.abspath("/Applications/Firefox.app/Contents/MacOS/firefoxdriver")
#os.environ["webdriver.firefox.driver"]=firefoxdriverpath
#driver=webdriver.Firefox(firefoxdriverpath)
#driver=webdriver.Firefox()
driver=webdriver.Firefox()
driver.get("http://www.baidu.com")
#点击打开搜索设置
driver.find_element_by_css_selector("#u1 > a[name='tj_settingicon']").click()
driver.find_element_by_css_selector("a.setpref").click()
#点击保存设置
driver.implicitly_wait(10)
#driver.find_element_by_css_selector("div#gxszButton a.prefpanelgo[href='#']").click()
driver.find_element_by_link_text("保存设置").click()
time.sleep(2)
#driver.find_element_by_css_selector("div#gxszButton a.prefpanelgo[href='#']")
#获取网页上的警告信息
#alert=driver.switch_to_alert().text()
if EC.alert_is_present:
print("Alert exists")
alert=driver.switch_to_alert()
print (alert.text)
alert.accept()
print("Alert accepted")
else:
print("NO alert exists")
'''
try:
WebDriverWait(driver,10).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')
print("0")
alert=driver.switch_to_alert().text()
print("1")
text=alert.text()
print(text)
except TimeoutException:
print("no alert")
#接收警告信息
#alert.accept()
#print("3")
#得到文本信息并打印
#alert=driver.switch_to_alert()
#print("5")
#取消对话框(如果有的话)
#alert=driver.switch_to_alert()
#alert.dismiss()
#输入值(如果有的话)
#alert=driver.switch_to_alert()
#alert.send_keys("xxx")
'''
driver.quit()
如果switch_to_alert不工作,最重要的问题就是,有1个以上的浏览器开启,导致alert抓取不到。并且在使用switch_to_alert的时候时间会比较长一些,需要等待一会儿才能完成accept等的工作。
原因是因为多个浏览器开启导致无法准确定位到哪个浏览器上,例如同时开启了两个firefox的浏览器,webdriver就无法定位到要测试的那个浏览器上,也就无法正常的获取到测试的那台浏览器上的alert窗口。
- python+selenium 使用switch_to_alert 出现的怪异常
- idea出现的怪异现象
- Python selenium的简单使用
- Python+Selenium--XPath的使用
- 安装selenium使用webdriver出现的问题
- 使用Selenium + Chromedriver出现的问题
- 记录一次使用_RecordsetPtr去访问已有表的新增字段时,出现的怪异问题!
- Selenium下的Python配合使用
- Python库selenium的安装与使用
- python写爬虫6-selenium的使用
- python下使用selenium的坑
- python web.py 的怪异重加载
- Python基础:那些怪异的函数
- android studio 使用以及常出现的问题
- python中使用selenium
- python selenium使用demo
- Python使用selenium出错
- Python Selenium使用IE
- mysql linux 区分大小写问题
- BAT 一键安装JDK
- 牛客网PAT题目1001——A+B和C(Java)
- 2018年及以后的物联网商业趋势
- 深度学习-->NLP-->Seq2Seq Learning(Encoder-Decoder,Beam Search,Attention)
- python+selenium 使用switch_to_alert 出现的怪异常
- CentOS查看进程、杀死进程、启动进程等常用命令
- Jdk1.7新特性
- 发版项目记录
- 无标题一
- Python自动化第五周-模块
- Windows下git命令下载caffe-segnet源代码
- 数据结构<四>: 递归
- 爬虫-知乎用户关系之关注