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.
Asked
Active
Viewed 9,772 times
4
-
On a server you control? Yes-set the appropriate headers. On a server you don’t control? Trust would defeat the purpose. You could set up a proxy so you’re not making Ajax requests, although you might have to play some games, and it still might not work. – Dave Newton Nov 29 '19 at 12:11
-
@DaveNewton thanks, the turn-around was rapid. ;) – VishalDevgire Nov 29 '19 at 12:16
-
Google didnt reveal much about the issue at hand – VishalDevgire Nov 29 '19 at 12:18
-
1Is it a server you control? – Dave Newton Nov 29 '19 at 12:54
1 Answers
12
While using ChromeDriver / Chrome combo to disable cors check you can use the --disable-web-security
argument.
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.
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:

undetected Selenium
- 183,867
- 41
- 278
- 352
-
1
-
1
-
1@AlexFilatov Your post was very helpful to explain the subject matter. Thanks for sharing your valuable research. – undetected Selenium Dec 27 '19 at 19:50