通过函数get_screenshot_as_file()来捕获错误截图,详细看下面举例:
#coding = utf-8 from selenium import webdriver import time #Selenium cookie处理 #D:chromedriver_win32chromedriver.exe #webdriver 可以帮助我们读取,添加,删除cookie信息。 class browser_1(object): def __init__(self): self.browser = webdriver.Chrome('D:chromedriver_win32chromedriver.exe') def browser_1(self): self.browser.get('https://www.baidu.com/') try: self.browser.find_element_by_id('kwsss').send_keys('selenium') self.browser.find_element_by_id('su').click() except: self.browser.get_screenshot_as_file(r"D:PycharmProjects1error_testerror_png.png") time.sleep(3) self.browser.quit() if __name__ == "__main__": a = browser_1() a.browser_1()