我需要在Chrome中安装和配置一个扩展,以便在Selenium测试执行期间修改所有请求头。我在Saucelabs中学习了这个支持文章的例子,展示了如何在本地为火狐做这件事,但不确定如何在Chrome上这样做。
ChromeDriver文档仅用于扩展的安装,而不是配置。
问题
计划是在SauceLabs上运行这个程序。将尝试使用ModHeader铬扩展设置所需的标头值。
编辑1
尝试安装Chrome版本的MODHeader扩展,但遇到了类似的问题。能够在本地安装扩展,但在远程执行中会看到一个错误。
private static IWebDriver GetRemoteDriver(string browser)
{
ChromeOptions options = new ChromeOptions();
options.AddExtensions("Tools/Chrome_ModHeader_2_0_6.crx");
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(ChromeOptions.Capability, options);
capabilities.SetCapability("name", buildContext);
capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");
capabilities.SetCapability(CapabilityType.Version, "");
capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
capabilities.SetCapability("screen-resolution", "1280x1024");
capabilities.SetCapability("username", "SaucelabsUserName");
capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
capabilities.SetCapability("build", "BuildNumber");
capabilities.SetCapability("seleniumVersion", "2.50.1");
return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}显示在SauceLabs日志中的错误是
[1.968][INFO]: RESPONSE InitSession unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: Arguments发布于 2016-05-31 11:09:25
既然您提到这个问题主要发生在远程,而且我注意到您正在使用SauceLabs,那么您从它们那里检查过本文吗?
Installing an Firefox Extension such as Modify Headers(You would need download the .xpi file on your machine first):
DesiredCapabilities caps = new DesiredCapabilities();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File("path\of\Modify Headers xpi file"));
profile.setPreference("general.useragent.override", "UA-STRING");
profile.setPreference("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
profile.setPreference("modifyheaders.headers.count", 1);
profile.setPreference("modifyheaders.headers.action0", "Add");
profile.setPreference("modifyheaders.headers.name0", "X-Forwarded-For");
profile.setPreference("modifyheaders.headers.value0", "161.76.79.1");
profile.setPreference("modifyheaders.headers.enabled0", true);
profile.setPreference("modifyheaders.config.active", true);
profile.setPreference("modifyheaders.config.alwaysOn", true);
profile.setPreference("modifyheaders.config.start", true);
caps.setCapability(FirefoxDriver.PROFILE, profile);
NOTE: If you trying to do the same using C#, you would need to use the ToBase64String() method.发布于 2017-11-18 16:42:54
public void AddHeaderChrome()
{
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("C:\\Downloads\\ModHeader_v2.0.9.crx"));
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(CapabilityType.options);
// launch the browser
WebDriver driver = new ChromeDriver(options);
String HeadersName[]=new String[10];
String HeadersValue[]=new String[10];;
int length;
if(ConfigDetails.HeadersName.contains(","))
{
HeadersName=ConfigDetails.HeadersName.split(",");
HeadersValue=ConfigDetails.HeadersValue.split(",");
length=HeadersName.length;
}
else
{
HeadersName[0]=ConfigDetails.HeadersName;
HeadersValue[0]=ConfigDetails.HeadersValue;
length=1;
}
int field_no=1;
for(int i=0;i<length;i++)
{
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/popup.html");
driver.findElement(By.xpath("//input[@id='fl-input-"+field_no+"']")).sendKeys(HeadersName[i]);
driver.findElement(By.xpath("//input[@id='fl-input-"+(field_no+1)+"']")).sendKeys(HeadersValue[i]);
field_no+=2
}发布于 2018-05-08 02:39:08
Chrome上的扩展有一个不变的唯一ID。
您可以使用selenium web驱动程序导航到chrome-extension://<EXTENSION_UUIF>/options.html,这里options.html是您定义的首选项页面。
然后执行脚本片段以更改存储在chrome.storage.local中的设置。
https://stackoverflow.com/questions/35027354
复制相似问题