美文网首页自动化测试Selenium系列
自动化测试实战--窗口切换 处理弹窗

自动化测试实战--窗口切换 处理弹窗

作者: 胆小的米老鼠 | 来源:发表于2018-08-31 13:10 被阅读157次

    在本例中所有用到的新方法:

    名称 作用
    current_window_handle 获得当前窗口句柄
    window_handles 返回的所有窗口的句柄到当前会话
    switch_to_window() 切换窗口

    切换窗口

    from  selenium import webdriver
    import time
    driver = webdriver.Firefox()
    driver.maximize_window()
    driver.implicitly_wait(3)
    url = 'http://www.baidu.com'
    driver.get(url)
    #获致当前页面的句柄
    search_hand= driver.current_window_handle
    print(search_hand)
    driver.find_element_by_link_text('登录').click()
    time.sleep(2)
    driver.find_element_by_link_text('立即注册').click()
    
    all_hands= driver.window_handles
    #进入注册页面
    for hand in all_hands:
        if hand != search_hand:
            driver.switch_to.window(hand)
            driver.find_element_by_id('TANGRAM__PSP_3__userName').send_keys('12333')
            time.sleep(1)
            driver.find_element_by_id('TANGRAM__PSP_3__phone').send_keys('1233390000')
    for hand in all_hands:
        if hand == search_hand:
            driver.switch_to.window(hand)
            driver.find_element_by_id('kw').send_keys('python')
            driver.find_element_by_class_name('bg_s_btn').click()
    driver.quit()
    
    

    处理弹窗

    名称 作用
    text 返回 alert/confirm/prompt 中的文字信息。
    accept 点击确认按钮。
    dismiss 点击取消按钮,如果有的话。
    from  selenium import webdriver
    import time
    from selenium.webdriver.common.action_chains import ActionChains
    driver = webdriver.Firefox()
    url = 'http://www.baidu.com'
    driver.get(url)
    driver.implicitly_wait(2)
    #鼠标悬停
    link = driver.find_element_by_link_text('设置')
    ActionChains(driver).move_to_element(link).perform()
    time.sleep(1)
    driver.find_element_by_class_name('setpref').click()
    time.sleep(2)
    driver.find_element_by_class_name('prefpanelgo').click()
    time.sleep(2)
    #接收弹窗
    driver.switch_to.alert.accept()
    driver.quit()
    print('执行完成')
    
    

    相关文章

      网友评论

        本文标题:自动化测试实战--窗口切换 处理弹窗

        本文链接:https://www.haomeiwen.com/subject/xshswftx.html