2012-08-29 47 views
6

Khi bạn đặt thông số máy chủ proxy trong mã bên dưới nếu máy chủ proxy của bạn yêu cầu xác thực thì FireFox sẽ mang hộp thoại Xác thực và về cơ bản bạn không thể tự động điền nó. Vì vậy, có cách nào để đặt USERNAMEPASSWORD?C# Selenium WebDriver Cấu hình FireFox - sử dụng proxy có xác thực

FirefoxProfile profile = new FirefoxProfile(); 
String PROXY = "192.168.1.100:8080"; 
OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy(); 
proxy.HttpProxy=PROXY; 
proxy.FtpProxy=PROXY; 
proxy.SslProxy=PROXY; 
profile.SetProxyPreferences(proxy); 
FirefoxDriver driver = new FirefoxDriver(profile); 

Nếu bạn cố gắng để định dạng chuỗi proxy để một cái gì đó giống như http://username:[email protected]:8080 mà Bạn nhận được lỗi chuỗi đó là không hợp lệ. Vì vậy, tôi tự hỏi có phải là một cách để đạt được điều này.

Mọi trợ giúp sẽ được đánh giá cao.

+0

Tôi chưa tìm thấy câu trả lời cho câu hỏi này, hiện tại tôi đã vô hiệu hóa xác thực trên máy chủ proxy của mình và cho phép nó theo phạm vi IP để sắp xếp công việc ngay bây giờ. – Tim

+0

Tôi cần tham chiếu gì cho 'ProfilesIni' khi gặp lỗi' Loại tên hoặc không gian tên 'ProfilesIni' không thể tìm thấy ' –

Trả lời

0

Việc bạn có thể làm là tạo hồ sơ và lưu dữ liệu xác thực vào đó. Nếu hồ sơ của bạn được gọi là "webdriver" bạn có thể chọn nó từ mã của bạn trong việc khởi tạo:

ProfilesIni allProfiles = new ProfilesIni(); 
FirefoxProfile profile = allProfiles.getProfile("WebDriver"); 
profile.setPreferences("foo.bar",23); 
WebDriver driver = new FirefoxDriver(profile); 
+0

Điều đó rất thú vị, tôi nên thử và báo cáo lại nếu nó hoạt động – Tim

+0

Tuyệt vời! Xin hãy thông báo cho tôi :) –

+0

Không gian tên nào là ProfilesIni? :/... –

2
 String PROXY = "http://login:[email protected]:port"; 
     ChromeOptions options = new ChromeOptions(); 

     options.AddArguments("user-data-dir=path/in/your/system"); 

     Proxy proxy = new Proxy(); 

     proxy.HttpProxy = PROXY; 
     proxy.SslProxy = PROXY; 
     proxy.FtpProxy = PROXY; 

     options.Proxy = proxy; 

     // Initialize the Chrome Driver 
     using (var driver = new ChromeDriver(options)) 
0

lại làm điều đó với MS UI Automation mà không AutoIt:

public void AuthInProxyWindow (string login, string pass) 
    { 
     var proxyWindow = AutomationElement.RootElement 
      .FindFirst(TreeScope.Subtree, 
       new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaDialogClass")); 

     var edits = proxyWindow.FindAll(TreeScope.Subtree, 
      new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); 

     var unamePoint = edits[1].GetClickablePoint(); 
     Mouse.MoveTo(new Point((int) unamePoint.X, (int) unamePoint.Y)); 
     Mouse.Click(MouseButton.Left); 

     SendKeys.SendWait(login); 
     var pwdPoint = edits[2].GetClickablePoint(); 
     Mouse.MoveTo(new Point((int) pwdPoint.X, (int) pwdPoint.Y)); 
     Mouse.Click(MouseButton.Left); 
     SendKeys.SendWait(pass); 

     Keyboard.Press(Key.Return); 
     Logger.Debug("Authefication in Firefox completed succesfully"); 
    } 

Chuột di chuyển bằng Microsoft.TestApi

Các vấn đề liên quan