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

Selenium - Mobile Emulation - how do I add user Agent to Chrome options while automating the 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);

您可以在 Chrome 网络浏览器中使用以下配置进行移动仿真:

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

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

// co.addArguments("mobileEmulation=" + mobileEmulation);
co.setExperimentalOption("mobileEmulation", mobileEmulation);
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