zoukankan      html  css  js  c++  java
  • selenium+python学习——弹出框

    1、alert警告框

    以百度搜索设置为例,在进行搜索设置后点击“保存设置”按钮,弹出alert对话框,如下图所示:

     实现代码:

    from selenium import webdriver
    import time as t
    from selenium.webdriver.support.select import Select
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as ec
    from selenium.webdriver.common.by import By
    
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get("http://www.baidu.com")
    t.sleep(3)
    driver.find_element_by_id("kw").send_keys("selenium")
    driver.find_element_by_id("su").send_keys(Keys.ENTER)
    # 显式等待,待元素定位
    WebDriverWait(driver,10).until(ec.presence_of_element_located((By.ID,"kw")))
    # 鼠标悬停
    mouse = driver.find_element_by_name("tj_settingicon")
    ActionChains(driver).move_to_element(mouse).perform()
    driver.find_element_by_link_text('搜索设置').click()
    # # 隐式等待
    # driver.implicitly_wait(10)
    t.sleep(5)
    # 定位下拉框
    select = driver.find_element_by_name("NR")
    # # 以索引的方式定位
    # Select(select).select_by_index(2)
    # print("下拉框选择的最新条数是:",select.get_attribute('value'))
    # 以value定位
    # Select(select).select_by_value("10")
    # print("下拉框选择的最新条数是:",select.get_attribute('value'))
    # 以文本定位
    Select(select).select_by_visible_text("每页显示10条")
    print("下拉框选择的最新条数是:",select.get_attribute('value'))
    t.sleep(3)
    driver.find_element_by_xpath('//*[@id="se-setting-5"]/label[1]').click()
    t.sleep(4)
    # 判断是否选中is_selected()
    s = driver.find_element_by_xpath('//*[@id="se-setting-5"]/label[1]').is_selected()
    if s == False:
        driver.find_element_by_xpath('//*[@id="se-setting-5"]/label[1]').click()
        print(s)
    else:
        print(True)
    driver.find_element_by_css_selector("#gxszButton > a.prefpanelgo").click()
    # alert弹框
    print("alert弹出框的文本信息为:{0}".format(driver.switch_to.alert.text))  # 获取弹出框的文本信息
    driver.switch_to.alert.accept()
    driver.quit()
    

      ps:driver.switch_to.alert后不加(),否则会出现TypeError

    accept()表示确认

    dismiss()表示取消

    2、confirm确认框

    显示:

     实现代码:

    from selenium import webdriver
    import time
    
    
    url = 'file:///C:/Users/yl8/Desktop/链接.html'
    driver = webdriver.Chrome()
    driver.get(url)
    time.sleep(4)
    # confirm操作
    driver.find_element_by_id("confirm").click()
    time.sleep(2)
    t = driver.switch_to.alert
    print(t.text)
    t.dismiss()
    

    3、prompt对话框

    类型:

    实现测试代码:

    from selenium import webdriver
    import time
    
    
    url = 'file:///C:/Users/yl8/Desktop/链接.html'
    driver = webdriver.Chrome()
    driver.get(url)
    time.sleep(4)
    # prompt操作
    driver.find_element_by_id("prompt").click()
    time.sleep(2)
    t = driver.switch_to.alert
    print(t.text)
    t.send_keys("hello selenium")
    t.accept()
    

      

     

  • 相关阅读:
    fzuoj Problem 2177 ytaaa
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
    zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces
    csuoj 1335: 高桥和低桥
  • 原文地址:https://www.cnblogs.com/yuer02/p/12625551.html
Copyright © 2011-2022 走看看