0

I have seen a lot of questions around setting the language of the Browser. But is there a way to set the browser's search result region to a specific country while launching the chromedriver?

I have no idea where to start- so I have tried to set the language of the browser(by the below code) but that doesn't solve my problem.

System.setProperty(“webdriver.chrome.driver”,”D:/DollarArchive/chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
options.addArguments(“–lang= sl”);
ChromeDriver driver = new ChromeDriver(options);
driver.get(“http://google.com");

I would need to know a way to set the region while initializing the ChromeDriver. When done manually -> Google -> Settings -> Search Setting -> Region Setting -> Select the desired country and Save.(as shown in the image below)

Region Setting in Google Chrome

But I wouldn't want to do that process through the UI. Hence I wanted to find out if there was a way to do it by ChromeOptions. Any thoughts?

S K
  • 308
  • 2
  • 6
  • 20
  • 1
    Try [How do I enable geolocation support in chromedriver for Selenium?](https://stackoverflow.com/questions/8411816/how-do-i-enable-geolocation-support-in-chromedriver-for-selenium) – Sers Sep 19 '18 at 18:19
  • @Sers I did try faking the Geolocation by running the Javascript after launching the browser but still doesn't return the Google Search Result for that specific country ----------------------------------- JavascriptExecutor js = (JavascriptExecutor) GlobalVars.driver; js.executeScript("window.navigator.geolocation.getCurrentPosition = function(success){" + "var position = {\"coords\" : {\"latitude\": \""+latitude+"\", \"longitude\": \""+longitude+"\"}}; " + "success(position);}"); – S K Sep 19 '18 at 19:10
  • That page in your screenshot is an HTML page... have you tried navigating to it and setting it yourself? You could also create a custom profile, set that setting, and then load the custom profile at the start of the script. – JeffC Sep 19 '18 at 19:36
  • @JeffC yes.. I have written a script to automate the process of navigating to the settings and selecting the desired country. But that just adds 3 seconds to the process. Could you elaborate on creating the custom profile? How would you suggest doing it? Is it via-- options.addArguments(“–lang= sl”);-- you say? if yes, I've tried that. If no, please elaborate. – S K Sep 19 '18 at 21:12
  • 1
    That's an argument passed to the browser, I'm talking about creating a specific profile with whatever settings you want and then loading that custom profile at the beginning of the script. You can google "java selenium chrome profile" and see what I'm talking about. – JeffC Sep 20 '18 at 01:53

2 Answers2

1

You can actually set the proxy for the browser driver instance to fetch search results for your desired region. For example, if I want search results for a specific region say Indonesia, I will set the INdonesia specific proxy for driver instance as below. You can find the list of free proxies for other regions of world here: https://free-proxy-list.net

The sample code snippet to set proxy will be as below:

    System.setProperty("webdriver.gecko.driver", "./Dependencies/geckodriver0.18.0");
    System.setProperty("webdriver.chrome.driver", "./Dependencies/chromedriver");

    Proxy p = new Proxy();
    p.setHttpProxy("103.66.44.19:53281");//replace this with your desired proxy
    p.setSslProxy("103.66.44.19:53281");//from https://free-proxy-list.net/

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(CapabilityType.PROXY,p);

    //For Chrome browser instance
    WebDriver driver = new ChromeDriver(caps);

    driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
    driver.get("https://www.google.com");
    Thread.sleep(20000);
    driver.quit();

    // For Firefox driver instance
    driver = new FirefoxDriver(caps);

    driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
    driver.get("https://www.google.com");
    Thread.sleep(20000);
    driver.quit();
Kireeti Annamaraj
  • 1,037
  • 1
  • 8
  • 12
0

Two ways

the simple way would be to add the country you want to search into the search field.... hey i said it was simple!

the fun way is not simple... Google generally uses your ip address to determine your location, and thats how it feeds you ads... and any 'local' data it has for you. your more likely to get a complete list of german items, if you have a german ip adress... and you can leave the language in english as well and let google translate do the lifting for you.

if you find an easy way to send a fake ip with your web request and get a result to come back to you i want to know so i can quit paying for a VPN

Technivorous
  • 1,682
  • 2
  • 16
  • 22