Multi-window switch and screenshots

Get the current window handle: b.current_window_handle

Get all window handle: b.window_handles

Switch to the appropriate window: 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/')
# Get the current window handle
current_handle = b.current_window_handle
# Print 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 ()



Screenshot

# 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()









Guess you like

Origin www.cnblogs.com/huaihe/p/11204383.html