Teach you how to use Python selenium to operate the basic browser object API

  • 2021-11-13 07:58:33
  • OfStack

Preface

Compared with various Selenium advanced guides on Gao Da, I personally think it is very important to lay a solid foundation.

In API of Selenium test framework, it is mainly divided into three categories:

1. Related operations on the browser itself.

2. Positioning the elements in the browser page.

3. After positioning the element in the browser page, one action on the element. Such as: click, input and other operations.

In this article, we first talk about the basic operation of Selenium framework on browser objects.

1. Import the Selenium library


#  Import Selenium Drive 
from selenium import webdriver

2. Create a browser object

That is, open a browser.


#  Syntax: driver = webdriver.xxx()
driver = webdriver.Chrome()

#  Use dir(driver) View the actions of browser objects 
print(dir(driver))

3. Browser window size setting


# 1. Setting Browser Size 
#  Width 480 , high 800( It is best to adjust separately according to the display )
driver.set_window_size(480, 800)

# 2. Get browser size 
driver.get_window_size()		

# 3. Maximize browser window (very common) 
driver.maximize_window()

Example:


"""
1. Learning objectives 
     Master selenium How to control the browser window size in 
2. Operation steps (method) 
    2.1 Set the browser window size, width and height 
        driver.set_window_size (Width, height) 
    2.2  Gets the browser window size 
        driver.get_window_size()
    2.3 Maximize the browser window 
        driver.maximize_window ) 
3. Demand 
     Use selenium Realize the setting of browser window size 
"""
# 1. Import seleniun
from selenium import webdriver
from time import sleep

# 2. Open Google Browser (get browser action object) 
driver = webdriver.Chrome()

# 3. Setting the browser window size 
# 3.1  Set the window to wide 100 , high 200
# (windowHandle Parameter is a window handle, which will be discussed later )
driver.set_window_size(100, 200)
sleep(3)

# 3.2  Gets the browser window size 
window_size = driver.get_window_size()
print(window_size)

# 3.3  Window maximization 
driver.maximize_window()

# 4. Close the browser 
driver.quit()
"""
 Output: 
{'width': 516, 'height': 200}
"""

4. Browser location settings


# 1. Get the browser location 
driver.get_window_position()		

# 2. Set the browser location 
driver.set_window_position(x,y)		

Note: The upper left corner of the display is (0,0) All position operations are displacement operations, in pixels, relative to the upper left corner of the display.

Example:


"""
1. Learning objectives 
     Master selenium The method of controlling the position of browser window in 
2. Operation steps (method) 
    2.1  Set the browser window position (horizontal and vertical coordinates) 
        set_window_position( Abscissa, ordinate )
    2.2  Get the browser window position 
        driver.get_window_position()

3. Demand 
     Use selenium Realize the setting of browser window position 
"""
# 1. Import seleniun
from selenium import webdriver
from time import sleep

# 2. Open Google Browser (get browser action object) 
driver = webdriver.Chrome()

# 3. Set the browser location 
# 3.1  Set the position of the window to 100 , 300
driver.set_window_position(100, 300)
sleep(2)

# 3.2  Get the browser window position 
window_position = driver.get_window_position()
print(window_position)

# 4. Close the browser 
driver.quit()

"""
 Output: 
{'x': 100, 'y': 300}
"""

5. Request access to Web site


#  Request a url
#  Syntax: driver.get(url)	
#  Writing at work 
url = "http://www.baidu.com"
driver.get(url)

Example:


"""
1. Learning objectives 
     Master selenium Controls the browser's access to the specified website in the 
2. Operation steps (method) 
     Request a url
     Syntax: driver.get(url)
3. Demand 
     Use selenium Realize the operation of accessing the specified website by the browser 
"""
# 1. Import seleniun
from selenium import webdriver
from time import sleep

# 2. Open Google Browser (get browser action object) 
driver = webdriver.Chrome()

# 3. Visit a Web site 
url = "http://www.baidu.com"
driver.get(url)
sleep(2)

# 4. Close the browser 
driver.quit()

6. Browser page forward, backward, and refresh


# 1. Page forward 
driver.forward()

# 2. Page backwards 
driver.back()

# 3. Page refresh 
driver.refresh()

Example:


"""
1. Learning objectives 
     Master selenium Control the forward, backward and refresh of the browser 
2. Operation steps (syntax) 
    2.1 Advance 
        driver.forward () 
    2.2 Backward 
        driver.back () 
    2.3 Refresh 
        driver.refresh () 
3. Demand 
     Use Google browser to open Baidu, JD.COM and Taobao respectively, and use forward, backward and refresh methods 
"""
# 1. Import selenium
from selenium import webdriver
from time import sleep

# 2. Open a browser --- Google Browser 
driver = webdriver.Chrome()
# 3. Window maximization 
driver.maximize_window()
sleep(2)
# 4. Enter the URL Baidu, JD.COM and Taobao 
driver.get("http://www.baidu.com")
sleep(2)
driver.get("http://www.jd.com")
sleep(2)
driver.get("http://www.taobao.com")
sleep(2)
# 5. Use forward, backward, refresh commands 
#  Advance 
driver.back()  #  Back to JD.COM 
sleep(2)
driver.back()  #  Back to Baidu 
sleep(2)
#  Backward 
driver.forward()  #  Advance to JD.COM 
sleep(2)
driver.forward()  #  Go to Taobao 
sleep(2)

#  Refresh 
driver.refresh()  #  Keep it on Taobao page 
sleep(2)

# 6. Close the browser 
driver.quit()

7. Close the browser

(1) Close the current window


#  Syntax: driver = webdriver.xxx()
driver = webdriver.Chrome()

#  Use dir(driver) View the actions of browser objects 
print(dir(driver))
0

(2) Exit the driver and close all associated windows


#  Syntax: driver = webdriver.xxx()
driver = webdriver.Chrome()

#  Use dir(driver) View the actions of browser objects 
print(dir(driver))
1

Related articles: