zoukankan      html  css  js  c++  java
  • Python-selenium,切换句柄及封装

    一、获取当前句柄及所有句柄

    handle=driver.current_window_handle  #获取当前窗口句柄
    print(handle)
    handles=driver.window_handles #获取所有窗口句柄
    print(handles)

    二、获取指定句柄,并封装成方法

    #coding=gbk
    import os
    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support.select import Select


    curent_path=os.path.dirname(__file__)
    driver=webdriver.Chrome()
    page_path=os.path.join(curent_path,'./pages/element_samples.html')
    driver.implicitly_wait(10)
    driver.get('file://'+page_path)

    handle=driver.current_window_handle #获取当前窗口句柄
    print(handle)
    handles=driver.window_handles #获取所有窗口句柄
    print(handles)

    ##封装
    def switch_window_by_title(title):
    for handle in driver.window_handles:
    driver.switch_to.window(handle)
    if driver.title.__contains__(title):
    break

    def switch_window_by_url(url):
    for handle in driver.window_handles:
    driver.switch_to.window(handle)
    if driver.current_url.__contains__(url):
    break


    ##小测试

    e=driver.find_element(By.XPATH,'//select[@name="jumpMenu"]')
    Select(e).select_by_visible_text("开封教育网")
    # switch_window_by_title("开封市教育体育网") ##切换到这个句柄
    switch_window_by_url('http://jtj.kaifeng.gov.cn/')
    driver.find_element(By.XPATH,'//a[text()="政务服务"]').click()
    
    
  • 相关阅读:
    旋转加载loading和点点加载loadingdemo
    css 点点加载demo
    gulp——myself配置
    AngularJS官网seed目录结构
    CSS content换行技术实现字符animation loading效果
    gulp入门与一些基本设置
    css 图标 旋转中
    【图文教程】WebStorm下使用Github下载以及上传代码
    gulp-uglify的使用
    面试题 ——— 二维数组的查找
  • 原文地址:https://www.cnblogs.com/123anqier-blog/p/12823803.html
Copyright © 2011-2022 走看看