Because in the actual test environment, the browser using the graphical interface occupies the test machine resources too much and the efficiency is low.Generally speaking, on the basis of debugging the script, the test case is executed without the browser graphical interface.
There are many headless browsers that can be used now. Here is an easy-to-use headless browser solution - use Chrome's disabled graphical interface to run.
Generally, when using Chrome for script execution, the browser object instantiated by the Chrome class is used. In fact, there is another class that provides the configuration of the browser - ChromeOptions( selenium.webdriver.ChromeOptions)
from selenium import webdriver
# Create the configuration needed to start the browser - instantiate the ChromeOptions browser options object
co = webdriver.ChromeOptions()
# Build configuration information - call the configuration method through the browser options object
co.headless = True # Settings The browser is headless mode
# Add configuration information to the browser startup - instantiate the browser driver object and add the attribute option value
driver = webdriver.Chrome(options=co)
# Set the browser page size to the common window size 1366*768
driver.set_window_size(1366, 768)
# Open the page and perform page operations
driver.get("http://www.baidu.com")
# Business operations
driver.find_element_by_id("kw").send_keys("chrome")
driver .get_screenshot_as_file("./a.png")
# Quit the browser driver object
driver.quit()
First: clarify the basic steps of Selenium to write automated test scripts
Simulate the user to open the browser - instantiate the browser-driven object
Simulate input URL operation-drive object calls get method
Simulate user page operations - drive object page positioning and element operations
Simulate the user to close the browser - the driver object calls the quit method
from selenium import webdriver
# 1. Simulate the user to open the browser - instantiate the browser driver object
driver = webdriver.Chrome()
# 2. Simulate the input URL operation - the driver object calls the get method
driver.get("http:// www.baidu.com")
# 3. Simulate user page operations - drive object page positioning and element operations
driver.find_element_by_id("kw").send_keys("chrome")
driver.get_screenshot_as_file("./a.png")
# 4. Simulate the user to close the browser - the driver object calls the quit method
driver.quit()
Then: extend the instantiation operation of the driver object - ChromeOptions
The above browser is the browser that is started by default - no configuration is done - how to realize the configuration and startup of the browser?
Create the configuration needed to start the browser - instantiate the ChromeOptions browser options object
Build configuration information - call the configuration method through the browser options object
Add configuration information to the browser startup - instantiate the browser driver object and add attribute option values
from selenium import webdriver
# Create the configuration needed to start the browser - instantiate the ChromeOptions browser options object
co = webdriver.ChromeOptions()
# Build configuration information - call the configuration method through the browser options object
co.headless = True # Settings The browser is headless mode
# Add configuration information to the browser startup - instantiate the browser driver object and add the attribute option value
driver = webdriver.Chrome(options=co)
# Open the page and perform page operations
driver.get("http ://www.baidu.com")
# Business operation
driver.find_element_by_id("kw").send_keys("chrome")
driver.get_screenshot_as_file("./a.png")
# Exit the browser driver object
driver.quit( )
Finally: optimize the above code, the window is too small to see the entire page - maximize the browser window
Note: There is an interesting phenomenon here: driver.maximize_window() does not work here
So you need to use another method to control the window size driver.set window size(x,y)
from selenium import webdriver
# Create the configuration needed to start the browser - instantiate the ChromeOptions browser options object
co = webdriver.ChromeOptions()
# Build configuration information - call the configuration method through the browser options object
co.headless = True # Settings The browser is headless mode
# Add configuration information to the browser startup - instantiate the browser driver object and add the attribute option value
driver = webdriver.Chrome(options=co)
# Set the browser page size to the common window size 1366*768
driver.set_window_size(1366, 768)
# Open the page and perform page operations
driver.get("http://www.baidu.com")
# Business operations
driver.find_element_by_id("kw").send_keys("chrome")
driver .get_screenshot_as_file("./a.png")
# Quit the browser driver object
driver.quit()
to sum up
If you need more browser settings, the best way is to read the code in the ChromeOptions class, you can learn a lot