zoukankan      html  css  js  c++  java
  • Python+webdriver自动化脚本初学记录之窗口切换(1)

    该脚本过程是打开百度网站,在输入框中输入“腾讯首页”并百度,在百度结果中点击腾讯首页进入腾讯网站窗口,在腾讯网站中点击NBA进入NBA窗口

    #coding = utf-8
    from selenium import webdriver
    import time
    driver = webdriver.Chrome()
    driver.maximize_window()
    time.sleep(3)
    driver.get('http://www.baidu.com/')
    time.sleep(3)
    driver.find_element_by_id('kw').send_keys('腾讯首页')
    driver.find_element_by_xpath("//input[@id='su']").click()
    time.sleep(3)
    driver.find_element_by_xpath("//*[@id='1']/h3/a[1]/em").click()
    time.sleep(7)
    window1=driver.current_window_handle #获取第一个窗口句柄
    windows = driver.window_handles #获取所有窗口句柄
    for current_window in windows:
         if current_window != window1:
              driver.switch_to.window(current_window)
    #使用循环判断句柄,如果当前句柄不是第一个窗口句柄(默认句柄指向的是第一个窗口句柄),切换到当前句柄。
    driver.find_element_by_link_text('NBA').click()
    time.sleep(10)
    driver.quit()
    print('测试通过')

  • 相关阅读:
    Codeforces Round #380(div 2)
    Codeforces Round #378(div 2)
    Codeforces Round #379(div 2)
    CCPC2016合肥现场赛
    CCPC2016沈阳站
    HDU2222 Keywords Search__AC自动机
    poj2185Milking Grid
    POJ2961_kmp
    POJ 2406
    poj 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/yaoze2018/p/9853672.html
Copyright © 2011-2022 走看看