• 下拉框+弹窗的处理


    下拉框+弹窗处理

    一、下拉框

    针对下拉框,selenium提供了Select类来处理

    from selenium.webdriver.support.select import Select
    

    1、实例化下拉框

    1. 定位到下拉框
    rp = chrome.find_element_by_name('rp')
    
    1. 实例化Select类
    select = Seletc(rp)
    

    2、定位

    • 索引(从0开始)定位(select_by_index())
    #定位到下拉框的第3个元素
    select.seletc_by_index(2)
    
    • value定位(select_by_value())
    #定位到下拉框value='25'的元素
    select.seletc_by_value(‘25’)
    
    • 文本定位(select_by_visible_text())
    #定位到下拉框文本为’30‘的元素
    select.select_by_visible_text(‘30’)
    

    二、弹窗

    针对弹窗,selenium提供了Alert类来处理

    from selenium.webdriver.common.alert import Alert
    

    1、告警框

    使用accept()确认

    #!/usr/bin/python3
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.alert import Alert
    import time
    
    chrome = webdriver.Chrome()
    chrome.maximize_window()
    chrome.get('https://www.baidu.com')
    shezhi = chrome.find_element_by_id('s-usersetting-top')
    ActionChains(chrome).move_to_element(shezhi).perform()
    chrome.find_element_by_link_text('搜索设置').click()
    time.sleep(2)
    chrome.find_element_by_link_text('保存设置').click()
    #获取告警框内容
    test = chrome.switch_to_alert().text
    print(test)
    #点击弹框中的确认
    chrome.switch_to_alert().accept()
    
    time.sleep(3)
    

    结果:
    已经记录下您的使用偏好

    2、确认框

    使用accept()确认,使用dismiss()拒绝

    #!/usr/bin/python3
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.alert import Alert
    import time
    
    chrome = webdriver.Chrome()
    chrome.maximize_window()
    chrome.get('https://www.w3school.com.cn/tiy/t.asp?f=js_confirm')
    chrome.switch_to_frame('iframeResult')
    chrome.find_element_by_xpath('/html/body/button').click()
    #获取确认框内容
    test = chrome.switch_to_alert().text
    print(test)
    
    #点击确认
    # chrome.switch_to_alert().accept()
    
    #点击拒绝
    chrome.switch_to_alert().dismiss()
    
    time.sleep(3)
    

    结果:
    Press a button!

    3、消息对话框

    使用send_keys()在消息对话框输入内容

    #!/usr/bin/python3
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.alert import Alert
    import time
    
    chrome = webdriver.Chrome()
    chrome.maximize_window()
    chrome.get('https://www.w3school.com.cn/tiy/t.asp?f=js_prompt')
    chrome.switch_to_frame('iframeResult')
    chrome.find_element_by_xpath('/html/body/button').click()
    #获取消息框内容
    # test = chrome.switch_to_alert().text
    # print(test)
    
    #消息框里输入内容
    chrome.switch_to_alert().send_keys('静心得意')
    # 确认
    chrome.switch_to_alert().accept()
    
    time.sleep(3)
    

    结果:

  • 相关阅读:
    SQL Server 数据库基础编程
    SQL Server 数据库设计
    SQL Server T-SQL高级查询(转)
    MVC组件分析(转)
    HTTP MIME类型即HttpResponse.ContentType属性值列表
    dreamweaver cs6 的破解方法
    varchar和Nvarchar区别
    .NET 可选择的转型路径(转)
    IT职场求生法则(转)
    HTML常见元素集锦
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13053171.html
Copyright © 2020-2023  润新知