Screenshots of windows
Automated use cases are executed by programs, so sometimes the error messages printed are not very clear. If you can save the screenshot of the current window when the script is executed incorrectly, you can see the cause of the error very intuitively through the picture. WebDriver provides four methods to intercept the current window: the screenshot function get_screenshot_as_file(), save_screenshot(), get_screenshot_as_png and get_screenshot_as_base64.
get_screenshot_as_file()
This method is very simple. By using the driver to get the method, we can write the path of the screenshot to be saved. First, we establish a Picture folder directory, in order to facilitate the storage and management of the screenshot pictures. The code is as follows:
from selenium.webdriver import Chrome from time import sleep # Visit Baidu driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe") driver.get("https://www.baidu.com/") driver.find_element_by_id("kw").send_keys("selenium") sleep(2)
# Save pictures to folders driver.get_screenshot_as_file("D:\\PythonProject\\Test\\Picture\\baidu.jpg") driver.quit()
When we run the above program, we find a Warning warning telling us that we recommend to save pictures in'.png'format, but it does not affect the normal process of saving pictures. As shown in the figure below.
Warning warnings are as follows: UserWarning: name used for saved screenshot does not match file type. It should end with a `png'extension
"type. It should end with a `.png` extension", UserWarning)
Optimize the. png format for saving pictures. Both preservations were successful!
save_screenshot()
The use of save_screenshot() is similar to the above get_screenshot_as_file(). Let's look directly at the examples. The code is as follows:
from selenium.webdriver import Chrome from time import sleep # Visit Baidu driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe") driver.get("https://www.baidu.com/") driver.find_element_by_id("kw").send_keys("selenium") sleep(2) # save_screenshot Save pictures driver.save_screenshot("D:\\PythonProject\\Test\\Picture\\baidu2.png") driver.quit()
get_screenshot_as_png and get_screenshot_as_base64
These two are not commonly used in peacetime, so you can get a general idea of them. get_screenshot_as_png is to acquire binary data stream, get_screenshot_as_base64 is to acquire Base64 encoding raw data. In practical work, it is summarized and collated again.
Close the browser
WebDriver provides two quit() and close() methods. Qut () exits the relevant driver and closes all windows. close() is used to close the current window. Example multi-window processing, in the process of use case execution
There are several windows open, and we want to close one of them, then we need to use the close() method to close it.
Let's take a chestnut and see what's the difference between close() and quit(). The code is as follows:
from selenium.webdriver import Chrome
from time import sleep
# Visit Baidu
driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.get("https://www.jd.com/")
# View the current window handle
indexwindow = driver.current_window_handle
print(indexwindow)
print('\n')
driver.find_element_by_link_text("Household Electric Appliances").click()
sleep(2)
#Loop through to find the first window,Open another "cell phone" page
for handle in driver.window_handles:
if handle == indexwindow:
driver.find_element_by_link_text("Mobile phone").click()
sleep(2)
# View all window handles
print(driver.window_handles)
print('\n')
# Close the current window
driver.close()
# Look at what's happening now window handles,You can see that it's just closing the first one. window,The other two window still
print(driver.window_handles)
print('\n')
# Close all windows and exit related drivers
driver.quit()
The results of the above code show that close() closes only one window that was initially opened, and two that are still open. As shown in the following figure:
You can try it. Practice it personally and remember it more deeply.~~~