简体   繁体   English

Python Selenium 4.3 无法在 Chromedriver 中使用网络数据获取性能日志

[英]Python Selenium 4.3 unable to get performace logs in Chromedriver with network data

I am trying to get the performance logs of Chromedriver to get access to all network data.我正在尝试获取 Chromedriver 的性能日志以访问所有网络数据。 The chromedriver is connecting to an Electron app. chromedriver 正在连接到 Electron 应用程序。 I tried all the solutions previously discussed here but none seem to work.我尝试了之前在这里讨论过的所有解决方案,但似乎都没有。

caps = DesiredCapabilities.CHROME
caps["goog:loggingPrefs"] = {'performance': 'ALL'}

service = Service(executable_path="./chromedriver.exe")
        
option=Options()
option.binary_location=path.abspath("./tsm-console-desktop.exe")
option.add_argument("remote-debugging-port=9515")


cdriver=webdriver.Chrome(service=service,options=option,desired_capabilities=caps)

I then run然后我跑

# Electron app loads the set url automatically
cdriver.get_log('browser')

All the answers say to run cdriver.get_log('performance') but Python complains it as invalid argument.所有答案都说要运行cdriver.get_log('performance')但 Python 抱怨它是无效的参数。

The output log does not have any method information, only console information output日志没有任何方法信息,只有控制台信息

{'level': 'SEVERE', 'message': 'https://dev.local.elektaplatform.com/otel-agent/v1/traces - Failed to load resource: the server responded with a status of 404 ()', 'source': 'network', 'timestamp': 1659614239337}

Any ideas?有任何想法吗?

To view the logs, make sure to have the following settings:要查看日志,请确保具有以下设置:

  1. Turn on logs by adding the following line to chromedriver.json:通过将以下行添加到 chromedriver.json 来打开日志:

"Logs": ["stdout"], 2) To get network data, add the following line to chromedriver.json: "Logs": ["stdout"], 2) 要获取网络数据,将以下行添加到 chromedriver.json:

"NetworkLogs": ["stdout"], 3) Add the following line to your command script: "NetworkLogs": ["stdout"], 3) 将以下行添加到您的命令脚本中:

driver.get_log('stdout') driver.get_log('stdout')

  1. Make sure that the logs are being sent to the console and to the file chromedriver.log确保将日志发送到控制台和文件 chromedriver.log

You might be able to get the performance logs by running the following command: chromedriver.get_log('performance') .您可以通过运行以下命令来获取性能日志: chromedriver.get_log('performance') Also, you may need to install the Python Imaging Library (PIL) in order to get the performance logs.此外,您可能需要安装 Python 映像库 (PIL) 以获取性能日志。

edit:编辑:

It appears that the version of Chromedriver that is bundled with Selenium has problems.看来Selenium捆绑的Chromedriver版本有问题。 I'm not sure if this is a bug in ChromeDriver or a bug in Selenium but it would be nice if they both worked with the bundled version of Chromedriver.我不确定这是 ChromeDriver 中的错误还是 Selenium 中的错误,但如果它们都使用 Chromedriver 的捆绑版本会很好。 I'm guessing it's a bug in ChromeDriver but I've never used it so can't be sure.我猜这是 ChromeDriver 中的一个错误,但我从未使用过它,所以不能确定。 The solution is to use the headless version of Chromedriver bundled with ChromeDriver and configure Selenium to use that rather than the bundled version.解决方案是使用与 ChromeDriver 捆绑的 Chromedriver 的无头版本,并将 Selenium 配置为使用该版本而不是捆绑版本。 That can be done by passing the following options to the WebDriver driver in the selenium server startup script:这可以通过将以下选项传递给 selenium 服务器启动脚本中的 WebDriver 驱动程序来完成:

webdriver.chrome.driver=path_to_chromedriver webdriver.chrome.binary=path_to_chrome webdriver.chrome.driver=path_to_chromedriver webdriver.chrome.binary=path_to_chrome

You can then use the ChromeOptions() method to select the chrome version.然后,您可以使用 ChromeOptions() 方法来 select chrome 版本。 on some systems, the binary was called chrome while the driver was called chromedriver .在某些系统上,二进制文件称为 chrome 而驱动程序称为chromedriver

To select the headless version of Chromedriver you can use this code:对于 select 无头版本的 Chromedriver,您可以使用以下代码:

chromeOptions = (webdriver.ChromeOptions()
.add_argument('--headless')
.set_value('1'))

This will add the headless option to the options for Chrome.这会将无头选项添加到 Chrome 的选项中。 You can then set the binary path using the binary option:然后,您可以使用 binary 选项设置二进制路径:

chromeOptions.binary = '/path/to/chromedriver'

You can then use the ChromeOptions() method to pass the options to the driver: chromeOptions.add_argument('--user-data-dir=path_to_cache') driver = webdriver.Chrome(chrome_options=chromeOptions)然后,您可以使用 ChromeOptions() 方法将选项传递给驱动程序: chromeOptions.add_argument('--user-data-dir=path_to_cache') driver = webdriver.Chrome(chrome_options=chromeOptions)

You will also need to configure the selenium server to use the path to the chromedriver binary in the selenium-server.properties file.您还需要配置 selenium 服务器以使用 selenium-server.properties 文件中的 chromedriver 二进制文件的路径。 You'll need to restart the server after making this change.进行此更改后,您需要重新启动服务器。 this can be a good config set up:这可能是一个很好的配置设置:

selenium.startup.chrome.driver=chromedriver.exe
selenium.startup.chrome.binary=/path/to/chromedriver
selenium.startup.chrome.options=-webdriver.chrome.driver=chromedriver.exe
webdriver.chrome.binary=/path/to/chromedriver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM