Nếu bạn muốn kích thước cửa sổ của trình duyệt thì bạn có thể sử dụng.
get_window_size[windowHandle='current']
Nhận chiều rộng và chiều cao của cửa sổ hiện tại.
Cách sử dụng: Driver.get_window_size []
Nhưng điều đó không giống với $[document].height[]
, mà bạn đã yêu cầu, vì vậy cách duy nhất để làm điều đó là kích hoạt cùng một lệnh JavaScript bằng cách sử dụng execute_script
.
from selenium import webdriver
driver = webdriver.PhantomJS[]
driver.get["//google.com"]
driver.maximize_window[]
height = driver.execute_script["return document.body.scrollHeight"]
print height
Lưu ý: Nếu bạn muốn thực thi lệnh jQuery, thì bạn sẽ phải làm bên dưới. If you want to execute the jQuery command, then you'll have to do below.
from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
trình điều khiển.manage.window.resize_to [chiều rộng, chiều cao]
trình điều khiển.set_window_size [chiều rộng, chiều cao]
- Tối đa hóa cửa sổ
- Thí dụ
- Đầu ra
- C#
- Thí dụ
- Đầu ra
- Tương tự trong ràng buộc C#, kích thước của cửa sổ trình duyệt có thể được tìm thấy bằng thuộc tính
4. Giao diện IWindow tương tự cũng xác định Phương thứcfrom selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
5 để tối đa hóa cửa sổ. Mặc dù giao diện này không cung cấp chức năng để thay đổi kích thước cửa sổ trực tiếp như Ruby Binding, nhưng nó có thể được thực hiện bằng cách đặt thuộc tínhfrom selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
6 bằng cách sử dụngfrom selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
7 Object1.from selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
- Thí dụ
- Đầu ra
- Tương tự trong ràng buộc C#, kích thước của cửa sổ trình duyệt có thể được tìm thấy bằng thuộc tính
4. Giao diện IWindow tương tự cũng xác định Phương thứcfrom selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
5 để tối đa hóa cửa sổ. Mặc dù giao diện này không cung cấp chức năng để thay đổi kích thước cửa sổ trực tiếp như Ruby Binding, nhưng nó có thể được thực hiện bằng cách đặt thuộc tínhfrom selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
6 bằng cách sử dụngfrom selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
7 Object1.from selenium import webdriver driver = webdriver.Firefox[] driver.get["//google.com"] driver.maximize_window[] with open['jquery-1.9.1.min.js', 'r'] as jquery_js: jquery = jquery_js.read[] #read the jquery from a file driver.execute_script[jquery] #active the jquery lib height = driver.execute_script["return $[document].height[]"] print height
- Môi trường được thử nghiệm: Windows 7, Selenium 2.39.0, Firefox 26.0
Tối đa hóa cửa sổ
trình điều khiển.Manage.Window.Maximize
trình điều khiển.Manage []. Window.Maximize [];
Mac OS Sierra, Ruby 2.3.1p112, Selenium 3.0.5, ChromDriver 2.26, GeckoDriver 0.13
Firefox 50.1, Chrome 55, PhantomJS 1.9.8
Thí dụ
require 'selenium-webdriver'
# get initial window size
driver = Selenium::WebDriver.for :firefox
puts driver.manage.window.size
# set window size using Dimension struct
target_size = Selenium::WebDriver::Dimension.new[1024, 768]
driver.manage.window.size = target_size
puts driver.manage.window.size
# resize window
driver.manage.window.resize_to[480, 320]
puts driver.manage.window.size
# maximize window
driver.manage.window.maximize
puts driver.manage.window.size
driver.quit
Đầu ra
# # # #
#
#
#
C#
Tương tự trong ràng buộc C#, kích thước của cửa sổ trình duyệt có thể được tìm thấy bằng thuộc tính
from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
4. Giao diện IWindow tương tự cũng xác định Phương thức from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
5 để tối đa hóa cửa sổ. Mặc dù giao diện này không cung cấp chức năng để thay đổi kích thước cửa sổ trực tiếp như Ruby Binding, nhưng nó có thể được thực hiện bằng cách đặt thuộc tính from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
6 bằng cách sử dụng from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
7 Object1.Môi trường được thử nghiệm: Windows 7, Selenium 2.39.0, Firefox 26.0
Windows 7, Selenium 2.39.0, Firefox 26.0
Thí dụ
using System;
using System.Drawing;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace BrowserWindowSizeApp {
internal class Program {
internal static void Main[string[] args] {
// get initial window size
IWebDriver driver = new FirefoxDriver[];
Console.WriteLine[driver.Manage[].Window.Size];
// set window size
driver.Manage[].Window.Size = new Size[480, 320];
Console.WriteLine[driver.Manage[].Window.Size];
// maximize window
driver.Manage[].Window.Maximize[];
Console.WriteLine[driver.Manage[].Window.Size];
driver.Quit[];
}
}
}
Đầu ra
# # # #
{Width=480, Height=320}
{Width=1804, Height=1096}
Tương tự trong ràng buộc C#, kích thước của cửa sổ trình duyệt có thể được tìm thấy bằng thuộc tính from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
4. Giao diện IWindow tương tự cũng xác định Phương thức from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
5 để tối đa hóa cửa sổ. Mặc dù giao diện này không cung cấp chức năng để thay đổi kích thước cửa sổ trực tiếp như Ruby Binding, nhưng nó có thể được thực hiện bằng cách đặt thuộc tính from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
6 bằng cách sử dụng from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
7 Object1.
from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
from selenium import webdriver
driver = webdriver.Firefox[]
driver.get["//google.com"]
driver.maximize_window[]
with open['jquery-1.9.1.min.js', 'r'] as jquery_js:
jquery = jquery_js.read[] #read the jquery from a file
driver.execute_script[jquery] #active the jquery lib
height = driver.execute_script["return $[document].height[]"]
print height
Môi trường được thử nghiệm: Windows 7, Selenium 2.39.0, Firefox 26.0
Môi trường được thử nghiệm: Cửa sổ 7, Python 2.7, Selenium 2.40.0, Firefox 26.0
Window 7, Python 2.7, Selenium 2.40.0, Firefox 26.0
Thí dụ
from selenium import webdriver
# get initial window size
driver = webdriver.Firefox[]
print driver.get_window_size[]
# set window size
driver.set_window_size[480, 320]
print driver.get_window_size[]
# maximize window
driver.maximize_window[]
print driver.get_window_size[]
driver.quit[]
Đầu ra
{u'width ': 1341, u'Height': 810} {u'width ': 480, u'Height': 320} {u'width ': 1804, u'Height': 1096}
{u'width': 480, u'height': 320}
{u'width': 1804, u'height': 1096}
So sánh
Ruby | Driver.Manage.Window.Size |
C# | trình điều khiển.Manage []. Window.Size; |
Python | trình điều khiển.get_window_size [] |
Ruby | Driver.Manage.Window.Size driver.manage.window.size = size |
C# | trình điều khiển.Manage []. Window.Size; driver.Manage[].Window.Size = windowSize; |
Python | trình điều khiển.get_window_size [] |
Ruby | Driver.Manage.Window.Size |
C# | trình điều khiển.get_window_size [] |
Python | trình điều khiển.get_window_size [] |
Ruby | Driver.Manage.Window.Size |
C# | trình điều khiển.Manage []. Window.Size; |
Python | trình điều khiển.get_window_size [] |
- Đặt kích thước cửa sổ
- size = selenium :: webdriver :: dimension.new [chiều rộng, chiều cao] trình điều khiển.manage.window.size = size
- System.Drawing.Size windowsize = new System.Drawing.Size [chiều rộng, chiều cao]; trình điều khiển.Manage []. window.size = windowsize;
- -