Selenium hoạt động như thế nào với nhiều tab trong python?

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
0

driver.current_window_handle
1

driver.current_window_handle
2

driver.current_window_handle
3

driver.current_window_handle
4

driver.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
6

driver.current_window_handle
7

driver.current_window_handle
8

driver.current_window_handle
9

driver.CurrentWindowHandle;
0

driver.CurrentWindowHandle;
1

Việ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;
2

driver.CurrentWindowHandle;
3

driver.CurrentWindowHandle;
4

driver.CurrentWindowHandle;
5

driver.CurrentWindowHandle;
6

driver.CurrentWindowHandle;
7

Nế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 WebDriver

driver.CurrentWindowHandle;
8

driver.CurrentWindowHandle;
9

await driver.getWindowHandle[];
0

await driver.getWindowHandle[];
1

await driver.getWindowHandle[];
2

await driver.getWindowHandle[];
3

WebDriver 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 thi

await driver.getWindowHandle[];
4

Quả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[];
5

await driver.getWindowHandle[];
6

await driver.getWindowHandle[];
7

await driver.getWindowHandle[];
8

await 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"]];
  
6

Nhậ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"]];
  
9

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"]]
  
0

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"]]
  
1

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"]]
  
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"]]
  
3

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"]]
  
4

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"]]
  
5

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"]]
  
6

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"]]
  
7

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"]]
  
8

Phó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"];
  
3

thu 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"];
  
8

Cử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'}
  
4

Chụ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];
  
0

TakeElementẢ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

Python xử lý các tab khác nhau trong Selenium như thế nào?

Cách chuyển tab trong Selenium cho Python .
Sau khi trình duyệt được khởi chạy và có nhiều tab đang hoạt động, hãy lưu trữ ID tay cầm cửa sổ của cửa sổ hiện đang hoạt động trong một biến bằng cách sử dụng phương thức current_window_handle
Lưu trữ ID xử lý cửa sổ của các tab đang hoạt động khác trong một biến bằng phương thức window_handles

Python xử lý nhiều cửa sổ trong Selenium như thế nào?

Xử lý nhiều cửa sổ con trong Selenium Python .
Nhấp vào liên kết “Phương thức bật lên cửa sổ'
Trong trang web mới, nhấp vào nút “Theo dõi Twitter & Facebook” để mở các cửa sổ con
Chuyển sang từng cửa sổ con đang mở bằng tiêu đề trang
In tiêu đề trang của mỗi cửa sổ con
Đóng phiên trình duyệt

Selenium xử lý nhiều trang như thế nào?

Các bước thực hiện. .
Lấy handle của cửa sổ cha bằng lệnh. Chuỗi parentWindowHandle = trình điều khiển. .
In tay cầm cửa sổ của cửa sổ cha
Tìm phần tử trên trang web bằng ID là công cụ định vị phần tử
Mở nhiều cửa sổ con
Lặp lại qua các cửa sổ con

Làm cách nào để xử lý nhiều trang trong Selenium Python?

Lưu trữ URL của trang trong một biến chuỗi page_url và tăng số lượng trang của nó bằng cách sử dụng bộ đếm vòng lặp for. Bây giờ, Khởi tạo trình duyệt web Chrome. Mở URL trang trong trình duyệt Chrome bằng đối tượng trình điều khiển. Bây giờ, Cạo dữ liệu từ trang web bằng cách sử dụng bộ định vị phần tử như phương thức find_elements_by_class_name

Chủ Đề