我正在研究Selenium 2& C#。我在IE9中有证书问题。我正在执行我的Selenium Test脚本并进入页面:“此网站的安全证书存在问题”。
当我尝试使用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 :(得分: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网址后调用certificate_continue()。这对ie9来说是有效的。
希望这有帮助
A