【问题标题】:Handle a popup window with selenium用硒处理弹出窗口
【发布时间】:2015-09-10 20:21:33
【问题描述】:

我尝试使用 selenium 在网页上自动下载。当我单击一个按钮时,会出现一个空白弹出窗口。大约 60 秒后,该弹出窗口上会出现一个链接。我想点击那个链接。这是我一直在使用的代码:

 //the button that makes the popup appear
 element = driver.SwitchTo().Frame(detailFrame).FindElement(By.XPath("//div[@class='smenu']/input[@value='Imprimer']"));
 //I save the current handle so that I can get back to it later
 currentHandle = driver.CurrentWindowHandle;
 //the finder is supposed to return the handle of the popup
 PopupWindowFinder finder = new PopupWindowFinder(driver);
 string popupWindowHandle = finder.Click(element);

 driver.SwitchTo().Window(popupWindowHandle);
 //I wait for the link to appear on the popup
 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(100));
 driver.FindElement(By.XPath("//body[@class='pop']/descendant::a[@href='/ppm/display_pdf.do']")).Click();
 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0));

这是弹出窗口的html代码:

<body class=pop onunload=closePopup();>
    <table class=table height="90%" align=center>
        <tbody>
            <tr class=center height="30%">
                <td rowSpan=”1” colSpan=”1”>
                    <a href="/ppm/display_pdf.do">Eng_X82.pdf </a>
                </td>
            </tr>
        </body>
    </table>

但是什么也没发生,我似乎无法选择要点击的链接。你看到我的代码有什么错误吗?

【问题讨论】:

  • 你的 html 不应该说 class="pop" (使用引号)吗?你是复制粘贴还是自己写的?
  • 嗨,它是一对复制和粘贴的组合。我在代码中的其他地方使用 ' 并且它可以工作
  • 我的建议:最好尽可能准确,否则对您的帮助是非常不同的,因为有很多方法会犯错误,我们需要首先排除尽可能多的可能性
  • 好的,这是个好建议!我使用此处找到的代码:stackoverflow.com/questions/27608921/…(最佳答案的“替代方式”)。

标签: c# html selenium


【解决方案1】:

你可以使用这样的东西。

Alert aBox =driver.switchTo().alert(); 
aBox.accept();

点击弹出的确定

Alert aBox =driver.switchTo().alert(); 
            aBox.dismiss();

点击弹出窗口的取消

【讨论】:

  • 不幸的是它没有点击链接。你知道我是否可以通过它的 url 选择一个窗口吗?
  • 警报是浏览器对话框,而不是他所指的 HTML 对话框。警报没有链接。
  • 确实如@JeffC 所说。该代码将适用于警报。它是处理警报的基本语法。如果是基于窗口的弹出窗口,则必须使用 AutoIT
猜你喜欢
  • 2020-04-20
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
相关资源
最近更新 更多