Selenium3 + Python3 automated test series eleven - and close your browser window Screenshot

Window Screenshot

  Automation cases by the program to be executed, so sometimes print an error message is not very clear. If you can save a screenshot of the current window when script execution error, it can be very intuitive to see the cause of the error through the pictures. Providing WebDriver theme function get_screenshot_as_file (), save_screenshot (), get_screenshot_as_png, get_screenshot_as_base64 four ways to intercept the current window.

get_screenshot_as_file()

   The very simple way, by obtaining driver this method, the path to save the screenshot write like, first create a Picture folder directory, in order to facilitate storage management screenshot picture. code show as below:

from selenium.webdriver import Chrome
from time import sleep

# 访问百度
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)
# 保存图片至文件夹中 driver.get_screenshot_as_file("D:\\PythonProject\\Test\\Picture\\baidu.jpg") driver.quit()

  When we run the above program, we have found a Warning warning, tell we recommend saving your image saved ".png" format, but does not affect the normal process to save the image. As shown below.

  Warning警告提示如下: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)

 

Optimization look for .png format to save the image. Twice to save all the success!

 save_screenshot()

  save_screenshot () method using the above get_screenshot_as_file () similar. We look directly instance. code show as below:

from selenium.webdriver import Chrome
from time import sleep

# 访问百度
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保存图片
driver.save_screenshot("D:\\PythonProject\\Test\\Picture\\baidu2.png")
driver.quit()

get_screenshot_as_png和get_screenshot_as_base64 

  The two usually are not commonly used, you can generally find out. get_screenshot_as_png is to obtain binary data stream, get_screenshot_as_base64 base64 encoding is to obtain the original data, the actual work in practice in case of re-summary finishing.

Close the browser

  WebDriver offers two quit () and close () method, quit () is the exit-related drivers and close all windows, close () is used to close the current window. Processing the multi-window embodiment, during the execution of the use case

Multiple windows are open, we want to close a window of them, then we should use close () method closed.

 

 

   Take a look at our chestnuts, close () and quit () What is the difference. code show as below:

from selenium.webdriver import Chrome
from time import sleep

# 访问百度
driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.get("https://www.jd.com/")

# 查看当前window handle
indexwindow = driver.current_window_handle
print(indexwindow)
print('\n')

driver.find_element_by_link_text("家用电器").click()
sleep(2)

#循环遍历找到第一个window,再打开一个“手机”页面
for handle in driver.window_handles:
    if handle == indexwindow:
        driver.find_element_by_link_text("手机").click()
sleep(2)
# 查看所有window handles
print(driver.window_handles)
print('\n')

# 关闭当前窗口
driver.close()

# 查看现在的所有window handles,可看到只是关闭了最开始的一个window,其他两个window还在
print(driver.window_handles)
print('\n')

# 关闭所有窗口,退出相关的驱动程序
driver.quit()

  上述代码运行结果可看出close()只关闭了最开始打开的一个window,后面打开的两个仍在。如下图所示:

 

 

   大家可以动手试试。亲自实践一下记忆更加深刻哦~~~

 

Guess you like

Origin www.cnblogs.com/wuweiblogs/p/11431133.html