WebDriver không phân biệt giữa cửa sổ và tab. Nếu trang web của bạn mở một tab hoặc cửa sổ mới, Selenium sẽ cho phép bạn làm việc với nó bằng một tay cầm cửa sổ. Mỗi cửa sổ có một mã định danh duy nhất vẫn tồn tại trong một phiên duy nhất. Bạn có thể lấy tay cầm cửa sổ của cửa sổ hiện tại bằng cách sử dụng
driver.getWindowHandle[];
driver.current_window_handle
driver.CurrentWindowHandle;
await driver.getWindowHandle[];
Chuyển đổi cửa sổ hoặc tab
Nhấp vào liên kết mở trong cửa sổ mới sẽ tập trung vào cửa sổ hoặc tab mới trên màn hình, nhưng WebDriver sẽ không biết cửa sổ nào mà Hệ điều hành cho là đang hoạt động. Để làm việc với cửa sổ mới, bạn sẽ cần chuyển sang cửa sổ đó. Nếu bạn chỉ có hai tab hoặc cửa sổ đang mở và bạn biết mình bắt đầu với cửa sổ nào, thì bằng quá trình loại bỏ, bạn có thể lặp qua cả hai cửa sổ hoặc tab mà WebDriver có thể nhìn thấy và chuyển sang cửa sổ hoặc tab không phải là bản gốc
Tuy nhiên, Selenium 4 cung cấp một api mới để tạo một tab mới [hoặc] cửa sổ mới và tự động chuyển sang nó
//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
# Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
________số 8_______
//Store the ID of the original window
val originalWindow = driver.getWindowHandle[]
//Check we don't have other windows open already
assert[driver.getWindowHandles[].size[] === 1]
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[]
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]]
//Loop through until we find a new window handle
for [windowHandle in driver.getWindowHandles[]] {
if [!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle]
break
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]]
Tạo cửa sổ mới [hoặc] tab mới và chuyển đổi
Tạo một cửa sổ [hoặc] tab mới và sẽ đặt tiêu điểm cho cửa sổ hoặc tab mới trên màn hình. Bạn không cần phải chuyển sang làm việc với tab [hoặc] cửa sổ mới. Nếu bạn có nhiều hơn hai cửa sổ [hoặc] tab được mở ngoài cửa sổ mới, bạn có thể lặp qua cả hai cửa sổ hoặc tab mà WebDriver có thể nhìn thấy và chuyển sang cửa sổ hoặc tab không phải là cửa sổ gốc
Ghi chú. Tính năng này hoạt động với Selenium 4 và các phiên bản mới hơn
driver.current_window_handle
0driver.current_window_handle
1driver.current_window_handle
2driver.current_window_handle
3driver.current_window_handle
4driver.current_window_handle
5Đóng cửa sổ hoặc tab
Khi bạn hoàn thành một cửa sổ hoặc tab và đó không phải là cửa sổ hoặc tab cuối cùng được mở trong trình duyệt của bạn, bạn nên đóng nó và chuyển về cửa sổ bạn đang sử dụng trước đó. Giả sử bạn đã làm theo mẫu mã trong phần trước, bạn sẽ có phần điều khiển cửa sổ trước đó được lưu trữ trong một biến. Đặt cái này lại với nhau và bạn sẽ nhận được
driver.current_window_handle
6driver.current_window_handle
7driver.current_window_handle
8driver.current_window_handle
9driver.CurrentWindowHandle;
0driver.CurrentWindowHandle;
1Việc quên quay lại tay cầm cửa sổ khác sau khi đóng cửa sổ sẽ khiến WebDriver thực thi trên trang hiện đã đóng và sẽ kích hoạt Ngoại lệ Không có Cửa sổ Như vậy. Bạn phải chuyển về một tay cầm cửa sổ hợp lệ để tiếp tục thực hiện
Thoát khỏi trình duyệt khi kết thúc phiên
Khi bạn kết thúc phiên trình duyệt, bạn nên gọi thoát, thay vì đóng
- bỏ di chúc
- Đóng tất cả các cửa sổ và tab được liên kết với phiên WebDriver đó
- Đóng quy trình trình duyệt
- Đóng quá trình điều khiển nền
- Thông báo cho Selenium Grid rằng trình duyệt không còn được sử dụng để phiên khác có thể sử dụng trình duyệt này [nếu bạn đang sử dụng Selenium Grid]
Việc không gọi thoát sẽ để lại các quá trình nền và cổng bổ sung đang chạy trên máy của bạn, điều này có thể gây ra sự cố cho bạn sau này
Một số khung kiểm tra cung cấp các phương thức và chú thích mà bạn có thể móc vào để xé nhỏ khi kết thúc kiểm tra
driver.CurrentWindowHandle;
2driver.CurrentWindowHandle;
3driver.CurrentWindowHandle;
4driver.CurrentWindowHandle;
5driver.CurrentWindowHandle;
6driver.CurrentWindowHandle;
7Nếu không chạy WebDriver trong ngữ cảnh thử nghiệm, bạn có thể cân nhắc sử dụng
//Store the ID of the original window
val originalWindow = driver.getWindowHandle[]
//Check we don't have other windows open already
assert[driver.getWindowHandles[].size[] === 1]
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[]
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]]
//Loop through until we find a new window handle
for [windowHandle in driver.getWindowHandles[]] {
if [!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle]
break
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]]
9 được hầu hết các ngôn ngữ cung cấp để một ngoại lệ vẫn sẽ dọn sạch phiên WebDriverdriver.CurrentWindowHandle;
8driver.CurrentWindowHandle;
9await driver.getWindowHandle[];
0await driver.getWindowHandle[];
1await driver.getWindowHandle[];
2await driver.getWindowHandle[];
3WebDriver của Python hiện hỗ trợ trình quản lý ngữ cảnh python, khi sử dụng từ khóa
driver.current_window_handle
00 có thể tự động thoát khỏi trình điều khiển khi kết thúc thực thiawait driver.getWindowHandle[];
4Quản lý cửa sổ
Độ phân giải màn hình có thể ảnh hưởng đến cách ứng dụng web của bạn hiển thị, vì vậy WebDriver cung cấp các cơ chế để di chuyển và thay đổi kích thước cửa sổ trình duyệt
Nhận kích thước cửa sổ
Tìm nạp kích thước của cửa sổ trình duyệt bằng pixel
await driver.getWindowHandle[];
5await driver.getWindowHandle[];
6await driver.getWindowHandle[];
7await driver.getWindowHandle[];
8await driver.getWindowHandle[];
9//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
0Đặt kích thước cửa sổ
Khôi phục cửa sổ và đặt kích thước cửa sổ
//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
1//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
2//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
3//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
4//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
5//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
6Nhận vị trí cửa sổ
Tìm nạp tọa độ của tọa độ trên cùng bên trái của cửa sổ trình duyệt
//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
7//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
8//Store the ID of the original window
String originalWindow = driver.getWindowHandle[];
//Check we don't have other windows open already
assert driver.getWindowHandles[].size[] == 1;
//Click the link which opens in a new window
driver.findElement[By.linkText["new window"]].click[];
//Wait for the new window or tab
wait.until[numberOfWindowsToBe[2]];
//Loop through until we find a new window handle
for [String windowHandle : driver.getWindowHandles[]] {
if[!originalWindow.contentEquals[windowHandle]] {
driver.switchTo[].window[windowHandle];
break;
}
}
//Wait for the new tab to finish loading content
wait.until[titleIs["Selenium documentation"]];
9from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
0from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
1from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
2Đặt vị trí cửa sổ
Di chuyển cửa sổ đến vị trí đã chọn
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
3from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
4from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
5from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
6from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
7from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
8Phóng to cửa sổ
phóng to cửa sổ. Đối với hầu hết các hệ điều hành, cửa sổ sẽ lấp đầy màn hình mà không chặn các menu và thanh công cụ của hệ điều hành
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Firefox[] as driver:
# Open URL
driver.get["//seleniumhq.github.io"]
# Setup wait for later
wait = WebDriverWait[driver, 10]
# Store the ID of the original window
original_window = driver.current_window_handle
# Check we don't have other windows open already
assert len[driver.window_handles] == 1
# Click the link which opens in a new window
driver.find_element[By.LINK_TEXT, "new window"].click[]
# Wait for the new window or tab
wait.until[EC.number_of_windows_to_be[2]]
# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window[window_handle]
break
# Wait for the new tab to finish loading content
wait.until[EC.title_is["SeleniumHQ Browser Automation"]]
9//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
0//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
1//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
2//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
3thu nhỏ cửa sổ
Thu nhỏ cửa sổ của bối cảnh duyệt web hiện tại. Hành vi chính xác của lệnh này dành riêng cho từng trình quản lý cửa sổ
Minimize Window thường ẩn cửa sổ trong khay hệ thống
Ghi chú. Tính năng này hoạt động với Selenium 4 và các phiên bản mới hơn
//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
4//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
5//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
6//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
7//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
8Cửa sổ toàn màn hình
Lấp đầy toàn bộ màn hình, tương tự như nhấn F11 trong hầu hết các trình duyệt
//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;
//Check we don't have other windows open already
Assert.AreEqual[driver.WindowHandles.Count, 1];
//Click the link which opens in a new window
driver.FindElement[By.LinkText["new window"]].Click[];
//Wait for the new window or tab
wait.Until[wd => wd.WindowHandles.Count == 2];
//Loop through until we find a new window handle
foreach[string window in driver.WindowHandles]
{
if[originalWindow != window]
{
driver.SwitchTo[].Window[window];
break;
}
}
//Wait for the new tab to finish loading content
wait.Until[wd => wd.Title == "Selenium documentation"];
9 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
0 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
1 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
2 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
3 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
4Chụp màn hình
Được sử dụng để chụp ảnh màn hình cho bối cảnh duyệt web hiện tại. Điểm cuối WebDriver trả về ảnh chụp màn hình được mã hóa ở định dạng Base64
# Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
5 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
6 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
7 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
8 # Store the ID of the original window
original_window = driver.window_handle
# Check we don't have other windows open already
assert[driver.window_handles.length == 1, 'Expected one window']
# Click the link which opens in a new window
driver.find_element[link: 'new window'].click
# Wait for the new window or tab
wait.until { driver.window_handles.length == 2 }
#Loop through until we find a new window handle
driver.window_handles.each do |handle|
if handle != original_window
driver.switch_to.window handle
break
end
end
#Wait for the new tab to finish loading content
wait.until { driver.title == 'Selenium documentation'}
9//Store the ID of the original window
const originalWindow = await driver.getWindowHandle[];
//Check we don't have other windows open already
assert[[await driver.getAllWindowHandles[]].length === 1];
//Click the link which opens in a new window
await driver.findElement[By.linkText['new window']].click[];
//Wait for the new window or tab
await driver.wait[
async [] => [await driver.getAllWindowHandles[]].length === 2,
10000
];
//Loop through until we find a new window handle
const windows = await driver.getAllWindowHandles[];
windows.forEach[async handle => {
if [handle !== originalWindow] {
await driver.switchTo[].window[handle];
}
}];
//Wait for the new tab to finish loading content
await driver.wait[until.titleIs['Selenium documentation'], 10000];
0TakeElementẢnh chụp màn hình
Được sử dụng để chụp ảnh màn hình của một phần tử cho bối cảnh duyệt web hiện tại. Điểm cuối WebDriver trả về ảnh chụp màn hình được mã hóa ở định dạng Base64