java - 在 Ant Design 中获取成功文本警报消息并使用 Selenium 进行验证

标签 java selenium xpath css-selectors webdriverwait

在我的一个项目中,我必须使用 Selenium 验证成功警报文本。 UI 按照 Ant 设计使用 React JS 进行编码。如需验证,请点击此链接“https://ant.design/components/message/” ',然后单击“显示正常消息”。该消息显示在页面顶部的上方,但我无法使用 Core Java 中的 Selenium Webdriver 编码从中获取文本。请帮忙。

我尝试了这段代码:

driver.findElement(By.cssSelector(".ant-btn-primary")).click();
            WebDriverWait wait = new WebDriverWait(driver, 60);
            WebElement successmessage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ant-message-custom-content-ant-message-success")));
            successmessage.getText();

最佳答案

要提取文本这是一条正常消息,您需要为visibilityOfElementLocated()引入WebDriverWait,并且您可以使用以下任一方法关注Locator Strategies :

  • css选择器:

    driver.get("https://ant.design/components/message/");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.ant-btn.ant-btn-primary"))).click();
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ant-message"))).getText());
    
  • xpath:

    driver.get("https://ant.design/components/message/");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='ant-btn ant-btn-primary']"))).click();
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ant-message']"))).getText());
    
  • 控制台输出:

    This is a normal message
    

关于java - 在 Ant Design 中获取成功文本警报消息并使用 Selenium 进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56730368/

相关文章:

python - selenium.common.exceptions.WebDriverException : Message: invalid session id using Selenium with ChromeDriver and Chrome through Python

xml - Xpath使用属性作为xml文件中的变量,其中有一个命名空间

java - 没有测试用例的 super JUnit 类

java - JOOQ 元模型 : why is there no Table. 重命名(字符串)?

java - 如何在 Android 中部署 Moquette 代理?

java - Selenium - WebDriver.findElement() 和 WebElement.findElement() 之间的区别

JavaFX 线程卡住

ruby - 使用 mechanize 检查名称相似但不同的 div

c# - 选择另一个旁边的特定节点 - C# - SelectSingleNode

java - fragment Activity 不膨胀