Python 无法在try/except中记录Selenium浏览器错误

Python 无法在try/except中记录Selenium浏览器错误,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在编写一个python脚本来测试我是否能够登录到url并验证页面上是否有元素 当出现任何类型的错误时,我希望从Selenium捕获日志 我的挑战是,每当我调用NoSuchElementException异常中的write\u logs函数时,写入的日志都是完全空白的 我有什么明显的遗漏吗 import os import json from selenium import webdriver from selenium.webdriver.common.desired_capabilitie

我正在编写一个python脚本来测试我是否能够登录到url并验证页面上是否有元素

当出现任何类型的错误时,我希望从Selenium捕获日志

我的挑战是,每当我调用
NoSuchElementException
异常中的
write\u logs
函数时,写入的日志都是完全空白的

我有什么明显的遗漏吗

import os
import json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException


# function that writes logs to file system
def write_logs(driver):
    log = driver.get_log('browser')
    with open('selenium_browser.log', 'w') as outfile:
        json.dump(log, outfile, indent=4)

baseurl = 'https://example.com'

username = os.environ.get('USER')
password = os.environ.get('PASSWORD')

xpaths = {'usernameTxtBox': "//input[@name='Username']",
          'passwordTxtBox': "//input[@name='Password']",
          'submitButton': "//input[@id='loginButton']"
          }
validate_url = 'https://example.com/page_only_accessible_once_logged_in'

d = DesiredCapabilities.CHROME
d['loggingPrefs'] = {'browser': 'ALL'}

driver = webdriver.Chrome(desired_capabilities=d)

driver.implicitly_wait(10)
driver.get(baseurl)

driver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()
driver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)

driver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()
driver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)
driver.find_element_by_xpath(xpaths['submitButton']).click()

try:
    logged_in = driver.find_element_by_xpath('//*[@id="idLoggedInTitle"]')
except NoSuchElementException:
    write_logs(driver)
    driver.close()
    exit("can't login")


try:
    driver.get(validate_url)
    element = driver.find_element_by_xpath('//*[@id="titleView!1Title"]')
    print(element.text)
except NoSuchElementException:
    write_logs(driver)
    driver.close()
    exit("Not able to reach url")

write_logs(driver)
driver.close()

print("completed sucessfully")

当前您正在使用
log=driver.get_log('browser')
创建日志,该日志只捕获浏览器错误,而不捕获selenium错误。如果您需要在搜索
驱动程序时记录selenium引发的
NoSuchElementException
。通过xpath('/*[@id=“titleView!1Title”]')
查找元素,则需要捕获回溯

import traceback

def write_logs(error_message, stack_trace):
    # print the error message and stack and see if that's what you want to log
    print (error_message + '\n' + stack_trace)
    # if it is, add it to your outfile how you want to record it
    with open('selenium_browser.log', 'w') as outfile:
        outfile.write(error_message + '\n' + stack_trace)

try:
    driver.get(validate_url)
    element = driver.find_element_by_xpath('//*[@id="titleView!1Title"]')
    print(element.text)
except NoSuchElementException as e:
    error_message = str(e)
    stack_trace = str(traceback.format_exc())
    write_logs(error_message, stack_trace)
    driver.close()
    exit("Not able to reach url")

您正在尝试记录python错误堆栈吗?或者您正在尝试记录浏览器控制台错误?正在从Selenium记录浏览器错误。更新问题以澄清“写入日志”功能中“驱动程序”参数的用途?没有
驱动程序
参数的功能,很好。将更新答案以删除它。