使用python的Selenium中current_window_handle和window_handles方法之间有什么区别?
Selenium中的current_window_handle和window_handles方法之间有区别。两者都是处理多个窗口的方法。他们的差异在下面列出-
current_window_handle
此方法获取当前窗口的句柄。因此,它现在处理焦点对准的窗口。它返回窗口句柄ID作为字符串值。
语法-
driver.current_window_handle
window_handles
此方法获取当前打开的窗口的所有句柄ID。窗口句柄ID的集合作为集合数据结构返回。
语法-
driver.window_handles w = driver.window_handles[2]
上面的代码提供了在当前会话中打开的第二个窗口的句柄ID。
示例
使用current_window_handle的代码实现
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://the-internet.herokuapp.com/windows")
#to refresh the browser
driver.refresh()
driver.find_element_by_link_text("Click Here").click()
#prints the window handle in focus
print(driver.current_window_handle)
#to close the browser
driver.quit()使用window_handles的代码实现。
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://the-internet.herokuapp.com/windows")
#to refresh the browser
driver.refresh()
driver.find_element_by_link_text("Click Here").click()
#prints the window handle in focus
print(driver.current_window_handle)
#to fetch the all handle ids of opened windows
chwnd = driver.window_handles;
# count the number of open windows in console
print("Total Windows : "+chwnd.size());
#to close the browser
driver.quit()热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短