webdriver无法单击带有证书错误页面的弹出窗口(webdriver not able to click pop up window with certificate error page)

编程入门 行业动态 更新时间:2024-10-25 04:17:25
webdriver无法单击带有证书错误页面的弹出窗口(webdriver not able to click pop up window with certificate error page)

使用webdriver 2.40.0(从nugget包安装)并在C#中编写代码我打开一个链接,为我的公司网站生成一个证书错误页面 - 单击覆盖链接元素以允许我继续访问该站点 - 单击一个输入该页面上的按钮生成一个弹出窗口,其上还有一个证书错误页面我的问题是当我尝试选择弹出窗口时抛出“noSuchWindowException”,代码:

namespace webDriverDemo { class Program { static void Main(string[] args) { string setURL = "xxxxx"; IWebDriver driver = new InternetExplorerDriver(@"C:\Drivers"); driver.Url = setURL; String loginPage = driver.CurrentWindowHandle; var securityLine = driver.FindElement(By.Id("overridelink")); if (!securityLine.Equals(null)) { securityLine.Click(); } var enterBtn = driver.FindElement(By.Id("EnterButton")); enterBtn.Click(); //Select the pop up window driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()"); driver.SwitchTo().Window("xxxx");

我试过了:

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

String riskPage = driver.CurrentWindowHandle;

并切换到那个窗口,我也试过了

driver.SwitchTo().Window();

但我认为问题是我无法访问证书错误页面的窗口名称,并且无法在该页面上选择和元素并尝试将其另存为单独的句柄。 真的需要帮助!

Using webdriver 2.40.0 (installed from nugget packages) and writing code in C# I am - opening a link for my company website which generates a certificate error page - clicking the override link element to allow me to continue to the site - clicking an enter button on that page which generates a pop up window that also had a certificate error page on top of it My problem is when I try to select the pop up window a “noSuchWindowException” is thrown, code:

namespace webDriverDemo { class Program { static void Main(string[] args) { string setURL = "xxxxx"; IWebDriver driver = new InternetExplorerDriver(@"C:\Drivers"); driver.Url = setURL; String loginPage = driver.CurrentWindowHandle; var securityLine = driver.FindElement(By.Id("overridelink")); if (!securityLine.Equals(null)) { securityLine.Click(); } var enterBtn = driver.FindElement(By.Id("EnterButton")); enterBtn.Click(); //Select the pop up window driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()"); driver.SwitchTo().Window("xxxx");

I’ve tried:

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

and

String riskPage = driver.CurrentWindowHandle;

and switching to that window, I’ve also tried

driver.SwitchTo().Window();

but I think the problem is that I can’t get to the window name of the certificate error page and cannot select and element on that page and try to save it as a separate handle. Really need help!

最满意答案

执行完操作后, enterBtn.Click(); 启动弹出窗口后,您需要将上下文切换到新窗口(使用它的窗口句柄,而不是标题)才能与之交互。

您可以从driver.WindowHandles列表中获取弹出窗口的句柄。

var riskPageHandle = driver.WindowHandles.FirstOrDefault(hwnd => hwnd != loginPageWindowHandle); if(riskPageHandle ==null) { //popup not found, log error or handle } else { //switch to the popup driver.SwitchTo().Window(riskPageHandle); Console.WriteLine("Popup window title is : " + driver.Title); //now accept the certificate error (your code, I haven't tried it) driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()"); }

Once you have performed the action enterBtn.Click(); that launches the popup window, you need to then switch context to the new window (using it's window handle, not title) to be able to interact with it.

You can obtain the popup window's handle from the driver.WindowHandles list.

var riskPageHandle = driver.WindowHandles.FirstOrDefault(hwnd => hwnd != loginPageWindowHandle); if(riskPageHandle ==null) { //popup not found, log error or handle } else { //switch to the popup driver.SwitchTo().Window(riskPageHandle); Console.WriteLine("Popup window title is : " + driver.Title); //now accept the certificate error (your code, I haven't tried it) driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()"); }

更多推荐

本文发布于:2023-08-05 08:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   弹出窗口   证书   错误   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!