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窗口。

最新文章

  1. Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement
  2. 点(x3,y3)到经过点(x1,y1)和点(x2,y2)的直线的最短距离
  3. Python File I/O
  4. javascript 老王开车去东北
  5. asp.net 调用前台JS调用后台,后台掉前台JS
  6. JS获取中文拼音首字母,并通过拼音首字母高速查找页面内的中文内容
  7. 使用Delphi声明C++带函数的结构体实战 good
  8. pixi.js
  9. oracle pctfree和pctused 详解
  10. 【Flask】 Jinja2模板语言
  11. DVB数字电视系统简介(DVB-C,DVB-S,DVB-T)
  12. Scrapy 爬虫日志中出现Forbidden by robots.txt
  13. vue中嵌套页面(iframe)
  14. 2018.09.29 bzoj3156: 防御准备(斜率优化dp)
  15. Qt绘图
  16. 问题 H: 抽奖活动(大数)
  17. linux awk 使用的一个例子
  18. js Array数组对象常见方法总结
  19. JAVA进阶----ThreadPoolExecutor机制(转)
  20. 设计模式之观察者模式(php实现)

热门文章

  1. spring配置与使用
  2. linux 之基本命令学习总结
  3. mysql5.7日志时间戳(log_timestmaps)与系统时间不一致问题以及日志报Got an error reading communication packets情况分析
  4. [Java]三大特性之封装
  5. Jquery树形控件 $.fn.zTree.init
  6. 067 Add Binary 二进制求和
  7. (转) cocos 里面scrollView一些方法
  8. 浅析libuv源码-node事件轮询解析(3)
  9. 执行ng build --prod --aot命令报错
  10. python之删除指定目录指定日期下的日志文件