python+selenium元素定位04——浏览器多窗口处理

1、获得新窗口句柄值

    每个窗口都会产生一个独立的句柄、随机的字符串(由字母和数字组成),每次打开句柄值都会变化

    

import os
import time
from selenium import webdriver
from selenium.webdriver.common.by  import By
from selenium.webdriver.support.select import Select

current = os.getcwd()
chrome_driver_path =os.path.join(current,'../webdriver/chromedriver')
page_path = os.path.join(current,'../pages/element_samples.html')    #本地html文件
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.implicitly_wait(2)
driver.get('file://'+page_path)
handle =driver.current_window_handle
print('当前窗口handle值:',handle)
e =driver.find_element(By.XPATH,'//select[@name="jumpMenu"]')
Select(e).select_by_visible_text('开封教育网')
time.sleep(3)
handles =driver.window_handles
print('所有的handle值',handles)
time.sleep(1)
driver.quit()

猜你喜欢

转载自www.cnblogs.com/miaoxiaochao/p/12639981.html
今日推荐