How do I disable WebRTC in Chrome Driver?

自闭症网瘾萝莉.ら 提交于 2021-02-18 18:53:57

问题


I want to disable WebRTC in Chrome Driver, but can't figure out how to do it.

I tried like this (found this solution in some blog):

capabilities.setCapability("chrome.switches", Arrays.asList(
   "--disable-webrtc-multiple-routes", "--disable-webrtc-hw-encoding",
   "--disable-webrtc-hw-decoding", "--disable-webrtc-encryption"));

It doesn't work. Yes, I know there isn't something like "--disable-webrtc" there, but it's all I have found about WebRTC, I mean, I didn't find some other WebRTC parameters.

I searched here: https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/chrome_switches.cc

And there: https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc

Didn't find anything about WebRTC.


回答1:


I found a solution.

I don't know how to disable WebRTC directly in Chrome Driver settings, but there is another way: add some WebRTC blocking extension. Here is how I did it:

  1. Launch you Chrome browser and search for Get CRX extension in the Chrome Webstore, install Get CRX.
  2. Now if you search for any other extension in the Chrome Webstore, open its page and right click at any place at the page, you'll see "Get CRX" at the context menu. Find your favorite WebRTC blocking extension, open its page and click "Get CRX". Download .crx file. Now you have WebRTC blocking extension in this .crx file and can use it in Chrome Driver.
  3. Use this code to launch Chrome Driver with the extension:

    Map<String, Object> chromeOptions = new HashMap<String, Object>();
    
    ArrayList<String> ext = new ArrayList<String>();
    byte[] byteExt = Files.readAllBytes(new File("path to .crx file").toPath());
    ext.add(Base64.encode(byteExt));
    chromeOptions.put("extensions", ext);
    
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    
    capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    
    WebDriver driver = new ChromeDriver(capabilities);
    

This code adds .crx extension to Chrome options. The extension must be base64 encoded. I use Apache Commons lib to encode it (import org.apache.xerces.impl.dv.util.Base64).

Now your Chrome Driver will launch with WebRTC blocking extension, so, WebRTC will be disabled.

But this method isn't ideal because in this case you will have WebRTC blocking extension in browser plugins. So, if you want to be completely random when testing some site (i.e. different ip, different window size, different user agent, different plugins) it won't be like this, because you will always have the same plugin.

So, if someone knows how to disable WebRTC without extensions, it will be highly appreciated.




回答2:


Here's a way to partially disable webRTC in Python. These are the options that webRTC extensions/plugins toggle in Chrome. These options prevent IP leak issues. Java is probably similar.

# partially disable webrtc
preferences = {
    "webrtc.ip_handling_policy" : "disable_non_proxied_udp",
    "webrtc.multiple_routes_enabled": False,
    "webrtc.nonproxied_udp_enabled" : False
}
chrome_options.add_experimental_option("prefs", preferences)

Works on Chrome 72, but should work on all versions 48 and up.



来源:https://stackoverflow.com/questions/44599265/how-do-i-disable-webrtc-in-chrome-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!