Hướng dẫn python selenium get page height - python selen lấy chiều cao trang

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.

Show

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("http://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("http://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
    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.get("http://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("http://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("http://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("http://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.
    • 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
    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.get("http://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("http://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("http://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("http://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

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("http://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("http://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("http://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("http://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("http://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("http://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("http://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("http://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

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

Nhận kích thước cửa sổ
RubyDriver.Manage.Window.Size
C#trình điều khiển.Manage (). Window.Size;
Pythontrình điều khiển.get_window_size ()
Đặt kích thước cửa sổ
RubyDriver.Manage.Window.Size
driver.manage.window.size = size
C#trình điều khiển.Manage (). Window.Size;
driver.Manage().Window.Size = windowSize;
Pythontrình điều khiển.get_window_size ()
Đặt kích thước cửa sổ
RubyDriver.Manage.Window.Size
C#trình điều khiển.get_window_size ()
Pythontrình điều khiển.get_window_size ()
Đặt kích thước cửa sổ
RubyDriver.Manage.Window.Size
C#trình điều khiển.Manage (). Window.Size;
Pythontrì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;
  • -

Làm cách nào để đặt chiều rộng và chiều cao trình duyệt trong selenium webdriver?

setSize (dem); Chúng tôi cũng có thể đặt chiều rộng và chiều cao trình duyệt với sự trợ giúp của các tùy chọn Chrome. Chúng ta phải tạo một đối tượng của lớp Chromeoptions và áp dụng các chất bổ sung cho nó. Bộ kích thước cửa sổ tham số với các giá trị có chiều cao và chiều rộng của trình duyệt được truyền dưới dạng đối số cho phương thức.create an object of the ChromeOptions class and apply addArguments to it. The parameter window-size set with values of height and width of the browser are passed as arguments to the method.

Làm cách nào để mở rộng một cửa sổ trong Selenium Python?

Chúng tôi có thể thay đổi kích thước cửa sổ trình duyệt trong selenium webdriver. Chúng ta có thể định cấu hình kích thước của trình duyệt với sự trợ giúp của phương thức SET_WINDOW_SIZE trong Python. Kích thước của kích thước cửa sổ được truyền dưới dạng tham số cho phương pháp này. Một lần nữa, để có được kích thước của trình duyệt, chúng ta có thể sử dụng phương thức get_window_size.configure the size of the browser with the help of the set_window_size method in Python. The dimensions of the window size are passed as parameters to this method. Again, to get the size of the browser, we can use the method get_window_size.

Làm thế nào để bạn cuộn xuống cuối một trang trong Selenium Python?

Bạn có muốn trình duyệt web cuộn đến cuối trang trong khi sử dụng python selenium không?Bạn có thể làm điều đó với mã, thủ thuật là tiêm mã JavaScript để trở thành trang web.Sau khi bạn tải một trang web, cuộn xuống trang bằng cách tiêm JavaScript.Bạn có thể cuộn xuống một lượng cụ thể hoặc tất cả các cách xuống đáy.inject Javascript code to be webpage. After you load a webpage, scroll down the page by injecting javascript. You can scroll down a specific amount or all the way to the bottom.

Làm thế nào để bạn quay lại một trang trong Python selenium?

Phương thức Back () được sử dụng để chuyển trở lại trang trình duyệt trước.Phương pháp này chỉ được áp dụng nếu chúng ta chuyển từ trang web này sang trang web khác.Chúng tôi cũng có thể quay trở lại trình duyệt với sự trợ giúp của một giám đốc điều hành JavaScript ở Selenium.Nó có phương thức Execute_Script () cho phép selen chạy các lệnh javascript.. This method only is applicable if we jump from webpage to another. We can also move back in the browser with the help of a Javascript Executor in Selenium. It has the execute_script() method which allows Selenium to run Javascript commands.