2013-08-01 45 views
13

我想用不同代理(iPhone,iPad,Android)測試用.NET編寫的Web應用程序,我正在使用NUnit進行測試和Selenium。如何更改硒中的用戶代理.net

任何人都有一個樣本來改變代理(例如iPad或iPhone)在硒與C#或VB?

回答

20

例UA(您可能需要谷歌自己找的那些適合你的目的):

的Mozilla/5.0(iPad版,CPU OS 6_0,例如Mac OS X )爲AppleWebKit/536.26(KHTML,例如Gecko)版本/ 6.0移動/ 10A5355d Safari瀏覽器/ 8536.25

C#的Firefox:

FirefoxProfile profile = new FirefoxProfile(); 
profile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"); 
IWebDriver driver = new FirefoxDriver(profile); 

C#鉻:

ChromeOptions options = new ChromeOptions(); 
options.AddArgument("--user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"); 
IWebDriver driver = new ChromeDriver(options); 

C#PhantomJS(未測試的代碼):

PhantomJSOptions options = new PhantomJSOptions(); 
options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"); 
IWebDriver driver = new PhantomJSDriver(options); 

C#IE:對不起,我不認爲這是可能的來自Selenium。

延伸閱讀:Set user agent using Selenium WebDriver C# and Ruby

3

在C#:

public static FirefoxProfile myFireProfile = new FirefoxProfile(); 
myFireProfile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10"); 
public IWebDriver driver= new FirefoxDriver(myFireProfile);