2012-07-04 35 views
2

我正在處理Selenium 2 & C#。我在IE9中有一個證書問題。我正在執行我的Selenium測試腳本並進入頁面:「此網站的安全證書存在問題」。如何解決此Selenium網站證書問題

當我嘗試點擊鏈接「Continue to this website(not recommended)」,使用:driver.FindElement(By.Id("overridelink"));時,Selenium沒有認出它,它無法點擊鏈接。

如果有人知道如何解決這個問題,請讓我知道嗎?

這是我的代碼:

DesiredCapabilities capabilities = new DesiredCapabilities(); 
capabilities.SetCapability(CapabilityType.AcceptSslCertificates, true); 
WebDriverObj = new InternetExplorerDriver(capabilities); 
MyBrowser = new WebDriverBackedSelenium(WebDriverObj, "http://www.google.com"); 
WebDriverObj.Navigate().GoToUrl("https://mywebsiteUrl"); 

WebDriverObj.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()"); 

IWebElement uname = WebDriverObj.FindElement(By.Id("ctl00_uxContentPlaceHolder_uxUsername")); 
uname.SendKeys("username"); 
IWebElement pwd = WebDriverObj.FindElement(By.Id("ctl00_uxContentPlaceHolder_uxPassword")); 
pwd.SendKeys("pass*"); 
+0

如果你打開該網站上即沒有硒 - 一切都OK了?你沒有得到證書問題? – RonK

+0

即使我在沒有Selenium的情況下打開IE,我也會得到Web證書安全性錯誤。 – Pat

+1

所以問題是網站本身和硒與它沒有太大關係。如果您正在使用某個網站,則應該可以創建一個自簽名的臨時證書。導入到IE瀏覽器應該解決您的問題。 – Arek

回答

0

這裏是Python版本,我發現某處:

def certificate_continue(): 
    """ 
    Find the IE Window that has a Certificate Error and try to continue anyway. 
    We'll use the win32 modules to find the right window & child window, 
    then write some Javascript into the address bar and execute to continue. 
    """ 
    def _enumWindowsCallback(hwnd, windows): 
     """ 
     This appends window information as a 3-tuple to the list 
     passed into win32gui.EnumWindows() 
     """ 
     class_name = win32gui.GetClassName(hwnd) 
     # apparently win32gui.GetWindowText() only works to get the text 
     # on a button or a label not really for edit windows. 
     text = win32gui.GetWindowText(hwnd) 
     windows.append((hwnd, class_name, text)) 


    def _get_certificate_error_window(): 
     """ 
     all_windows[] gets filled up with a list of tuples, then loop through 
     it filtering on class and the window text (title bar text). 
     Assumes only one 'Certificate Error' window. 
     """ 
     all_windows = [] 
     win32gui.EnumWindows(_enumWindowsCallback, all_windows) 
     for win in all_windows: 
      class_name = win[1] 
      title_bar_text = win[2] 
      if class_name == 'IEFrame' and \ 
        'Certificate Error: Navigation Blocked' in title_bar_text: 
       return win 

    def _get_edit_text(hwnd): 
     buf_size = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) 
     buf_size += 1 # don't forget that null character boys... 
     buffer = win32gui.PyMakeBuffer(buf_size) 
     win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buffer) 
     # don't need the null character now for Python 
     return buffer[:buf_size] 


    def _get_address_bar(parent_handle): 
     """ 
     There appears to be several 'Edit' windows within each browser window. 
     From Microsoft: If a child window has created child windows of its own, 
     EnumChildWindows enumerates those windows as well. 
     """ 
     childwins = [] 
     win32gui.EnumChildWindows(parent_handle, _enumWindowsCallback, 
            childwins) 
     for win in childwins: 
      child_handle = win[0] 
      class_name = win[1] 
      if 'Edit' in class_name: 
       edit_text = _get_edit_text(child_handle) 
       if 'http://' in edit_text or 'https://' in edit_text: 
        return child_handle # then this must be it... 

# begin certificate_continue 
    target_win = _get_certificate_error_window() 
    try: 
     cert_err_handle = target_win[0] 
    except TypeError: 
     print "OK, no Certificate Error window available" 
     return(1) 

    address_bar_handle = _get_address_bar(cert_err_handle) 
    # any better way to check the handle ? 
    if not win32gui.IsWindow(address_bar_handle): 
     print "Choked getting IE edit window" 
     return(1) 

    # now, need to send this JavaScript text to the browser Address Bar 
    javascript_continue = 'javascript: var continue_element = document.getElementById("overridelink"); continue_element.click();' 
    win32gui.SendMessage(address_bar_handle, win32con.WM_SETTEXT, 0, 
        javascript_continue) 

    # OK, and finally, send a carriage return to the address bar 
    win32gui.SendMessage(address_bar_handle, win32con.WM_KEYDOWN, 
        win32con.VK_RETURN, 0) 
    return(0) 

現在只需撥打certificate_continue()調用driver.get到SSL網址後。這與ie9一起工作(工作)。

希望這有助於

一個