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. 51nod1101(dp)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1101 题意:中文题诶- 思路:dp 我们用dp[i][j]存 ...

  2. springboot 启动

    1. 新建一个java 类,名为Application,代码内容: @ServletComponentScan@SpringBootApplicationpublic class Applicatio ...

  3. IDEA热部署自动重启服务问题

    新接手项目过于庞大,从eclipse果断换成了IDEA.因为IDEA有免费的热部署,咳咳咳... 但是,手贱的我在下面这张状态下,直接OK了,TM,这不是我要的热部署啊,这是重新启动啊,但是勾选了do ...

  4. js函数-构成

    前言 函数是一种封装,在任何语言中都是一个核心概念.在js中,函数是做为对象的子类型存在的.可以拥有自己的属性和方法,可以做为值进行传递,这两个特性让js拥有使用函数式编程的能力. 函数的声明 字面量 ...

  5. 浅谈JAVA GUI中,AWT与Swing的区别、联系及优缺点

    浅谈JAVA GUI中,AWT与Swing的区别.联系及优缺点 A.区别 1.发布的时间 AWT是在JDK 1.0版本时提出的 Swing是在AWT之后提出的(JAVA 2) 2. ”重量” AWT是 ...

  6. [NOI2002] 贪吃的九头蛇

    考虑任意一种划给大头的方案,两端的都给了大头(bel=1)的边产生难受值,剩下n-k个果子分给m-1个头,当m-1=1时,两端都给了这个小头也产生难受值:而m-1>1的情况要好看的多,贪心的,因 ...

  7. Java 多线程高并发编程 笔记(二)

    1. 单例模式(在内存之中永远只有一个对象) 1.1 多线程安全单例模式——不使用同步锁 public class Singleton { private static Singleton sin=n ...

  8. ignoring option MaxPermSize=256m; support was removed in 8.0 - unsupported JVM property

    MaxPermSize 在JVM8 中已经被删除. 可以使用 JAVA_OPTS="-Xms1303m -Xmx1303m -XX:MaxPermSize=256m 参考 https://b ...

  9. Sublime Text 快捷键的使用

    Sublime Text 快捷键的使用 快捷键列表(Shortcuts Cheatsheet) 我把本文出现的Sublime Text按其类型整理在这里,以便查阅. 通用(General) ↑↓←→: ...

  10. UVaLive6443(线段树)

    要点 题意--题意往往是个大坎Orz:输入操作 p 则在区间\([x_1,x_2]\)插入一个三次函数, t 则先查询区间\([x_1,x_2]\)的函数值的和,然后按题目要求得到新的\(x_1\). ...