c# - 如何使用 C# 和 Selenium 处理简单的警报?
问题描述
这是我的代码,当它到达时Switchto().Alert()
出现错误提示“没有这样的警报”
driver.FindElement(By.XPath("(.//*[normalize-space(text()) and normalize-space(.)='Add Document'])[1]/following::button[1]")).Click();
**driver.SwitchTo().Alert().Accept();**
var signFrame = driver.FindElement(By.Id("hsEmbeddedFrame"));
driver.SwitchTo().Frame(signFrame);
driver.FindElement(By.XPath("(.//*[normalize-space(text()) and normalize-space(.)='Istrong textnitials'])[1]/preceding::li[1]")).Click();strong text
解决方案
您可能需要等待警报存在才能单击它:
using OpenQA.Selenium.Support.UI;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
// alert is present will automatically switch to the alert
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());
// the below line is no longer necessary
// driver.SwitchTo().Alert().Accept();
如果这仍然抛出NoSuchAlertException
,那么出现的弹出窗口可能不是真正的警报,而是可能是 HTML 模式——在这种情况下,您可以检查它并使用 Selenium 找到合适的选择器来接受它。
推荐阅读
- java - 如何在循环之外重新打印循环输出
- kubernetes - URL 上的连接超时异常 - http://localhost:8888 & 找不到 PropertySource:GET 请求上的 I/O 错误
- php - 查询自定义分类术语的所有帖子
- spring - spring starter 项目中的服务 url 是什么?默认服务 url https://start.spring.io 的替代方案
- android - 如何解决代码中的 .settext 错误?
- jquery - Blueimp jQuery File Upload 不会验证或调整客户端图像的大小
- javascript - es6模块路径和依赖的处理方式
- sql - 在 SQL 表中插入 RANK
- java - 在 ubuntu 的 netbeans 中启用 javaFx
- java - eclipse中的java程序错误