python-判断alter是否存在

from selenium import webdrivercss

 

import timeweb

from selenium.webdriver.support.ui import WebDriverWait浏览器

from selenium.webdriver.support import expected_conditions as ECapp

from selenium.webdriver import firefoxcurl

from selenium.webdriver.common.keys import Keyspost

 

 

 

 

 

#firefoxdriverpath=os.path.abspath("/Applications/Firefox.app/Contents/MacOS/firefoxdriver")测试

#os.environ["webdriver.firefox.driver"]=firefoxdriverpathui

#driver=webdriver.Firefox(firefoxdriverpath)url

#driver=webdriver.Firefox()spa

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

相关文章
相关标签/搜索