2

我正在研究 Selenium 2 和 C#。我在 IE9 中有证书问题。我正在执行我的 Selenium 测试脚本并进入以下页面:“此网站的安全证书存在问题”。

当我尝试使用:单击“继续访问此网站(不推荐)”链接时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*");
4

1 回答 1

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)

现在只需在调用 driver.get 到 SSL url 后调用 certificate_continue()。这对我来说用 ie9 工作(工作)。

希望这可以帮助

一种

于 2012-07-23T08:32:02.110 回答