在我使用带有Python的Selenium Webdriver打开网站后,我试图获取 HTTP状态代码,我发现无法获得 HTTP状态代码,所以唯一的方法是获取网络日志并从中获取 HTTP状态代码
所以我试图用硒打印日志,但这给了我 InvalidArgumentException
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser logging
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'performance':'ALL' }
driver = webdriver.Chrome(desired_capabilities=d)
# load the desired webpage
driver.get('http://foo.com')
# print messages
for entry in driver.get_log('performance'):
print(entry)
这是运行后出现的错误
InvalidArgumentException Traceback (most recent call last)
<ipython-input-13-8480733201dc> in <module>
12
13 # print messages
---> 14 for entry in driver.get_log('performance'):
15 print(entry)
c:\users\slimshady\appdata\local\programs\python\python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py in get_log(self, log_type)
1260 driver.get_log('server')
1261 """
-> 1262 return self.execute(Command.GET_LOG, {'type': log_type})['value']
c:\users\slimshady\appdata\local\programs\python\python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
c:\users\slimshady\appdata\local\programs\python\python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
InvalidArgumentException: Message: invalid argument: log type 'performance' not found
(Session info: chrome=75.0.3770.80)
在这里我可能做错了什么?
答案 0 :(得分:3)
根据{3}在Chrome驱动程序75中的指定,功能loggingPrefs
已重命名为goog:loggingPrefs
,这是W3C标准所要求的。因此,应该对设置功能的代码进行调整,至少由于日志捕获原因,没有必要退回到非w3c模式。
d['goog:loggingPrefs'] = { 'performance':'ALL' }
答案 1 :(得分:1)
chrome 75及其chromedriver遇到相同的问题,将'w3c'设置为False可解决此问题:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'performance':'ALL' }
chrome_options = Options()
chrome_options.add_experimental_option('w3c', False)
driver = webdriver.Chrome(desired_capabilities=d, options=chrome_options)