zoukankan      html  css  js  c++  java
  • python-判断alter是否存在

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

  • 相关阅读:
    Service Broker between two instance
    第一次调用Web service响应速度慢的解决办法
    MVC静态化
    WinFrom Exception Handler
    The difference for ROW_NUMBER/RANK/DENSE_RANK/NTILE
    字符编码
    TSQL中的参数与变量
    Entity Framework Power Tool
    Use ActionFilterAttribute to check user logging
    SiteFactory技术实现QQ群19452522用户交流贴
  • 原文地址:https://www.cnblogs.com/thb-blog/p/7614266.html
Copyright © 2011-2022 走看看