3

Below are the capabilities I added. I am getting a Google reCAPTCHA in my website which can be trespassed by adding user agent.

But even after the addition of user agent I am still getting the captcha. Is there another way to add it?

Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Pixel 2");

Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("mobileEmulation", mobileEmulation);

chromeOptions.put("args",
                  Arrays.asList("disable-bundled-ppapi-flash",
                  "disable-extensions",
                  "profile-directory=Default",
                  "disable-plugins-discovery",
                  "--user-agent=" + userAgent));

ChromeOptions co = new ChromeOptions();
co.addArguments("mobileEmulation="+mobileEmulation);

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptions);

System.setProperty("webdriver.chrome.driver", RunConfig.CHROME_DRIVER_EXE);

driver = new ChromeDriver(capabilities);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
testergirl
  • 121
  • 1
  • 4
  • 11

3 Answers3

2

You can use the below configuration for mobile emulation in the Chrome web browser:

Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 1078);
deviceMetrics.put("height", 924);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 8.0.0;" +
"Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML,
like Gecko) " +
"Chrome/67.0.3396.99 Mobile Safari/537.36");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions);

Instead of add argument setExpermentalOption to be used

// co.addArguments("mobileEmulation=" + mobileEmulation);
co.setExperimentalOption("mobileEmulation", mobileEmulation);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Jain
  • 4,389
  • 2
  • 18
  • 21
  • There is a seperate user agent for the website which I am working on. It helps to trespass the captcha. Any idea on where to add it here? – testergirl Sep 11 '18 at 12:20
  • There would be a setting that captcha will not appear for particular useragent. To send request from that useragent the code which I have given will work. It works for me, The main thing which works here is below point which is missing in your code chromeOptions.setExperimentalOption("mobileEmulation",mobileEmulation); – Amit Jain Sep 11 '18 at 14:59
  • See the example how useragent is passed here mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 8.0.0;" + "Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/67.0.3396.99 Mobile Safari/537.36"); – Amit Jain Sep 17 '18 at 10:09
0
public class mobileEmulatorTest {

    public static WebDriver driver;

    @Before
    public void setUp(){
        WebDriverManager.chromedriver().setup();
    }

    @Test
    public void emulatorTest() throws InterruptedException {
        Map<String, String> mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceName", "iPhone 6");

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

        driver = new ChromeDriver(chromeOptions);
        driver.get("https://www.google.co.uk");
    }
}

Add WebDiver Manager dependency https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager

Pushparaj
  • 92
  • 4
0
public class DeviceEmulation {

    @Test
    public void deviceemulation() {
        Map<String, String> mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceName", "Pixel 2");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("mobileEmulation", mobileEmulation);
        options.addArguments("remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get(helper.Pages.Home);
        Demohelper.pause();
    }
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
Manasi
  • 21