Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there way to disable CORS check using RemoteWebDriver for SauceLabs

Question says it all, I m trying to execute some selenium tests on SauceLabs, the test loads a webpage that makes a cross domain request. I was thinking is there a way to disable CORS, in platform-independent way through code.

like image 406
VishalDevgire Avatar asked Sep 06 '25 03:09

VishalDevgire


1 Answers

While using ChromeDriver / Chrome combo to disable cors check you can use the --disable-web-security argument.

sameoriginpolicy

which is defined in content_switches.cc as:

// Don't enforce the same-origin policy. (Used by people testing their sites.)
const char kDisableWebSecurity[]            = "disable-web-security";

Code samples:

  • Windows:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-web-security"); // don't enforce the same-origin policy
    options.addArguments("--disable-gpu"); // applicable to windows os only
    options.addArguments("--user-data-dir=~/chromeTemp"); // applicable to windows os only
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://google.com");
    
  • OSX:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-web-security"); // don't enforce the same-origin policy
    options.addArguments("--user-data-dir=/tmp/chrome_dev_test");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://google.com");
    
  • Linux

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-web-security"); // don't enforce the same-origin policy
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://google.com");
    

Note: If you need access to local files for development/testing purposes like AJAX or JSON, you can use -–allow-file-access-from-files flag.

allowfileaccess


References

  • Disable same origin policy in Chrome
  • Disable-web-security in Chrome 48+
  • Run Chrome browser without CORS

Outro

You can find a couple of relevant discussions in:

  • Uncaught DOMException: Blocked a frame with origin “http://localhost:8080” from accessing a cross-origin frame while listing the iframes in page
  • Error: Permission denied to access property “x” due to same/cross origin policy using Selenium?
like image 133
undetected Selenium Avatar answered Sep 07 '25 20:09

undetected Selenium