Selenium-移动仿真-自动仿真器时如何在Chrome选项中添加用户代理? [英] Selenium - Mobile Emulation - how do I add user Agent to Chrome options while automating the emulator?

查看:180
本文介绍了Selenium-移动仿真-自动仿真器时如何在Chrome选项中添加用户代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我添加的功能.我收到了Google reCAPTCHA 在我的网站上,可以通过添加用户代理来侵入.

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

推荐答案

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

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

代替使用添加参数setExpermentalOption

Instead of add argument setExpermentalOption to be used

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

这篇关于Selenium-移动仿真-自动仿真器时如何在Chrome选项中添加用户代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆