python+selenium 使用switch_to_alert 出现的怪异常

'''

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值