title: Python selenium accessing chrome dev tools with execute_cdp_cmd | Determine which stylesheetId belongs to which stylesheet
body: <p>I am using selenium with python to automatically remove unused CSS code from my website.</p>
  <p>I found a solution which seems to be good here:</p>
  <p><a href="https://chromedevtools.github.io/devtools-protocol/tot/CSS/" rel="nofollow noreferrer">https://chromedevtools.github.io/devtools-protocol/tot/CSS/</a></p>
  <p>I have tried now different ways to generate the ranges json with the code I use and this seems to be promising:</p>
  <pre><code>browser.execute_cdp_cmd(&quot;CSS.enable&quot;, {})
  browser.execute_cdp_cmd(&quot;CSS.startRuleUsageTracking&quot;, {})
  
  sleep(1)
  browser.execute_script(&quot;window.scrollTo(0, document.body.scrollHeight);&quot;)
  sleep(1)
  
  #snapshot = browser.execute_cdp_cmd(&quot;Profiler.takePreciseCoverage&quot;, {})
  #snapshot = browser.execute_cdp_cmd(&quot;Profiler.stop&quot;, {})
  #browser.execute_cdp_cmd(&quot;CSS.stopRuleUsageTracking&quot;, {})
  snapshot = browser.execute_cdp_cmd(&quot;CSS.takeCoverageDelta&quot;, {}) # seems to work best as it is dynamic
  print(snapshot)
  
  with open('coverage_delta_css.json', 'w', encoding='utf-8') as _file:
      json.dump(snapshot, _file)
  
  print(snapshot['coverage'][0]['styleSheetId']) # a test id
  print(browser.execute_cdp_cmd(&quot;CSS.getStyleSheetText &quot;, {&quot;styleSheetId&quot;: snapshot['coverage'][0]['styleSheetId']})) # here it tells me the Id is unknown
  </code></pre>
  <p>Here is an example part of the generated json:</p>
  <pre><code>{
    &quot;coverage&quot;: [
      {
        &quot;endOffset&quot;: 335,
        &quot;startOffset&quot;: 133,
        &quot;styleSheetId&quot;: &quot;16752.5&quot;,
        &quot;used&quot;: true
      },
      {
        &quot;endOffset&quot;: 1025,
        &quot;startOffset&quot;: 471,
        &quot;styleSheetId&quot;: &quot;16752.7&quot;,
        &quot;used&quot;: true
      },
      ...
    ]
  }
  </code></pre>
  <p>The problem is that <code>styleSheetId</code> is a number and I can't find a way how to determine which stylesheet it refers to. I have (<code>main.css</code> and <code>other.css</code>). I want to remove unused CSS only in other CSS.</p>
  <p>Also in the example above I try to get the RAW text of the stylesheet with the id in the JSON, but it seems that the id changes with every call and is unknown.</p>
  <pre><code>selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {&quot;code&quot;:-32601,&quot;message&quot;:&quot;'CSS.getStyleSheetText ' wasn't found&quot;}
  </code></pre>
  <p>I feel like I am close to the solution. Hope someone can help on the last steps.</p>