【问题标题】:Capturing a number from an alert in selenium webdriver从 selenium webdriver 中的警报中捕获数字
【发布时间】:2021-03-07 18:38:30
【问题描述】:

我想捕获此处显示的警报中显示的数字。我曾尝试使用xpath 单击它,但它不起作用。感谢任何帮助。

WebElement attribute = driver.findElement(By.xpath("//span[@class='text']//a"));

【问题讨论】:

  • 显示在哪里?如果是弹出式警报,您将无法使用该方法
  • 它显示在用户界面上...这是我检查警报时控制台中显示的信息.. 成功 "您已成功创建文件”9988987
  • 我没有看到任何屏幕截图或任何控制台示例
  • 如果来自浏览器控制台,请检查:stackoverflow.com/questions/25431380/…
  • 我尝试添加截图,但它不允许我添加截图。第一次在这里发帖。不知道为什么它一直拒绝截图。

标签: java selenium


【解决方案1】:

如果是浏览器javascript alert,需要使用这个:

//Click the link to activate the alert
driver.findElement(By.linkText("See an example alert")).click();

//Wait for the alert to be displayed and store it in a variable
Alert alert = wait.until(ExpectedConditions.alertIsPresent());

//Store the alert text in a variable
String text = alert.getText();

//Press the OK button
alert.accept();

【讨论】:

  • 非常感谢。我会试试这个。
  • 只是为了澄清,通过提醒我了解浏览器打开的弹出窗口(您通常可以阻止的弹出窗口)
  • 我的脚本创建了一个文件,保存后它会显示一个警报 - 带有文件编号...我试图捕获此编号以用于继续使用它的脚本。顺便说一句,我使用了你的代码,我得到了 - org.openqa.selenium.remote.ErrorCodes toStatus '没有这样的警报'(预期为 400)404 -> 不正确的 JSON 状态映射为'没有这样的警报'(预期为 400)。
【解决方案2】:

首先,单击警报链接。然后尝试下面的代码(它将切换到当前的警报框并从中获取文本)。

driver.switchTo().alert().getText();

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 2021-09-02
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多