Selenium-移动仿真-如何在自动化仿真器时将用户代理添加到Chrome选项?

时间:2018-09-11 10:20:25

标签: java selenium chrome-options mobile-emulator

以下是我添加的功能。我收到了Google reCaptcha             在我的网站上,可以通过添加用户代理来侵入。但是,即使             添加用户代理后,我仍然会收到验证码。是             还有其他添加方式吗?

            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);

2 个答案:

答案 0 :(得分:0)

您可以使用以下配置在chrome Web浏览器上进行移动仿真

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);

@ Edit-而不是添加要使用的参数setExpermentalOption

// co.addArguments("mobileEmulation="+mobileEmulation);
co.setExperimentalOption("mobileEmulation",mobileEmulation);

答案 1 :(得分: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");
    }
}

添加WebDiver Manager 依赖项 https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager