多窗口切换及截图

获得当前窗口句柄:b.current_window_handle

获取所有窗口的句柄:b.window_handles

切换到相应的窗口:switch_to_window()

# coding:utf-8
from selenium import webdriver
from time import sleep
import time
b=webdriver.Chrome()
b = webdriver.Chrome()
#b.implicitly_wait(10)
b.maximize_window()
b.get('http://wenku.baidu.com/')
# 获取当前窗口句柄
current_handle = b.current_window_handle
# 打印当前窗口句柄
print current_handle
b.find_element_by_name('word').send_keys('userName')
b.find_element_by_link_text(u'小学').click()
all_handle = b.window_handles
print all_handle
for handle in all_handle:
if handle != current_handle:
b.switch_to.window(current_handle)
# 切换到原来的窗口
b.find_element_by_xpath("//*[@id='wk-all-cate']/dl[1]/dd/a[4]").click()
b.switch_to.window(current_handle)
b.find_element_by_name('word').clear()
b.find_element_by_name('word').send_keys('lisi')
sleep(5)
#截图操作
b.get_screenshot_as_file(r'D:\123.png')

b.quit()



截图

# b = webdriver.Chrome()
# b.implicitly_wait(10)
# b.maximize_window()
# b.get("http://www.baidu.com")
# b.get_screenshot_as_file('./baidu.jpg')
# #img_ele = b.find_element_by_id('su')
# #img_ele.screenshot(r".//test.png")
# sleep(3)
# b.quit()









猜你喜欢

转载自www.cnblogs.com/huaihe/p/11204383.html