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-判断alter是否存在的更多相关文章

  1. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

  2. 【Python备忘】python判断文件和文件夹是否存在

    python判断文件和文件夹是否存在 import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果 ...

  3. python 判断连个 Path 是否是相同的文件夹

    python 判断连个 Path 是否是相同的文件夹 import os os.path.normcase(p1) == os.path.normcase(p2) normcase() 在 windo ...

  4. Python判断列表是否已排序的各种方法及其性能分析

    目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...

  5. python 判断变量是否存在 防止报错

    Python判断变量是否存在 方法一:使用try: ... except NameError: .... try: var except NameError: var_exists = False e ...

  6. python 判断是否为中文

    python在执行代码过程是不知道这个字符是什么意思的.是否是中文,而是把所有代码翻译成二进制也就是000111这种形式,机器可以看懂的语言. 也就是在计算机中所有的字符都是有数字来表示的.汉字也是有 ...

  7. (转)python 判断数据类型

    原文:https://blog.csdn.net/mydriverc2/article/details/78687269 Python 判断数据类型有type和isinstance 基本区别在于: t ...

  8. python判断字符串是否为空的方法s.strip()=='' if not s.strip():

    python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='':     print 's is null' 或者 if not s.strip():     p ...

  9. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  10. Python判断字符串编码以及编码的转换

    转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...

随机推荐

  1. UE4中资源加载资源的方式

    在UNITY中,我们加载资源一般是通过Resources.Load(path).即可完成.该方法返回的是Object类型.如果你想要的是材质或者贴图等等,只要价格类型转换的关键字就可以了例如 as M ...

  2. PHP AES/ECB 128加密

    class Security { public static function encrypt($input, $key) { $key=md5($key.md5($key)); $size = mc ...

  3. 2017-9-20 NOIP模拟赛

    A 约数之和 (count.pas/c/cpp)TL:1S ML:128MB[Description]我们用 D(x)表示正整数 x 的约数的个数.给定一个正整数 N,求 D(1)+D(2)+…+D( ...

  4. MyBatis日志实现

    maven项目resources文件夹下log4j.properties 其作用是输出controller包下参与Mybatis的类的SQL语句输出.如果包名不一样,请根据自己的项目情况调整. # G ...

  5. react-native-syan-image-picker的使用

    传送门 第一种方式:link 第一步:安装 1.  npm install react-native-syan-image-picker --save 2.  react-native link re ...

  6. CC33:碰撞的蚂蚁

    题目 在n个顶点的多边形上有n只蚂蚁,这些蚂蚁同时开始沿着多边形的边爬行,请求出这些蚂蚁相撞的概率.(这里的相撞是指存在任意两只蚂蚁会相撞) 给定一个int n(3<=n<=10000), ...

  7. PKUSC 2018 题解

    PKUSC 2018 题解 Day 1 T1 真实排名 Link Solution 考虑对于每一个人单独算 每一个人有两种情况,翻倍和不翻倍,他的名次不变等价于大于等于他的人数不变 设当前考虑的人的成 ...

  8. Exploring refit, an automatic type-safe REST library for .NET Standard

    自动类型安全的.NET标准REST库refit   在SCOTT HANSELMAN 博客上看到一个好东西<Exploring refit, an automatic type-safe RES ...

  9. Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型的准确率(图文详解)

    不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的 ...

  10. jQuery dataTable 操作个人使用总结

    用过之后总会忘,不停的查,不停的忘.这里记录一下,仅为个人简单总结,具体使用方法请看官方API文档. 1. 获取表中行数.  var rowNum = $(tableSelector).DataTab ...