尝试打印错误消息

时间:2018-04-17 08:31:48

标签: java selenium-webdriver

我正在尝试打印页面上出现的错误消息错误消息是:

  

ERROR   您的请求未成功。您需要联系客户服务中心以安排更换。

现在上面的错误消息“联系客户服务”是一个重定向到另一个页面的链接。

我尝试过以下方法来获取文字:

//  WebElement text = driver.findElement(By.xpath("//a[contains(text(),'contact Customer Care')]"));
or
//  WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Your request has not been successful. You need to')]"));
or
//  WebElement text = driver.findElement(By.xpath("//a[@href='/app/contact']"));
or
WebElement text = driver.findElement(By.xpath("//div[@id='SubmitFormError']"));

System.out.println(text.getText());

什么都没有用。它不打印任何东西。

代码是:

<div id="SubmitFormError" class="tab-pane fade in active">
<p></p>
<p id="yui_3_17_2_1_1523943995346_146">Your request has not been successful. You need to
 <a href="/app/contact" class="link-dark-bold" id="yui_3_17_2_1_1523943995346_145">contact Customer Care</a> to arrange a replacement.
</p>
<p></p>

</div>

2 个答案:

答案 0 :(得分:2)

您好我刚刚从头开始制作了一个项目,其中包含您提供的所有信息。如果您执行以下操作似乎有效(index.html是您在上面提供的html-snipped)。请根据您的需要调整setupPage()

public class MyTest extends Selenide {

    @Rule
    public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();
    private static WebDriver webDriver;

    @Before
    public void setupPage() {
        Configuration.reportsFolder = "build/reports/integrationTest/screenshots";
        Configuration.browser = WebDriverRunner.PHANTOMJS;
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setJavascriptEnabled(true);
        caps.setCapability(PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "assets/driver/phantomjs_mac");
        String[] phantomJsArgs = { "--ignore-ssl-errors=true" };
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, phantomJsArgs);
        caps.setCapability("takesScreenshot", true);
        webDriver = new PhantomJSDriver(caps);
        webDriver.manage().window().setSize(new Dimension(1280, 800));
        WebDriverRunner.setWebDriver(webDriver);
        open("file:///Users/UserName/Desktop/index.html");
    }

    @Test
    public void test() {
        WebElement text = webDriver.findElement(By.xpath("//div[@id='SubmitFormError']/p[2]/a"));

        System.out.println(text.getText());

    }
}

在我的情况下,它正确打印:contact Customer Care

您现在可以致电text.click();并点击/app/contact的链接,在那里您可以阅读新的Elements并执行操作。我会将text重命名为更有意义的内容。

顺便说一下:我从daveoncode的评论中获取了/ p [2]。

如果这是您在本课程中唯一的考试,请记得在考试结束时添加webDriver.quit();退出驱动程序。

答案 1 :(得分:0)

将html代码复制/粘贴到本地文件中:

<html>
    <head>
    </head>
    <body>
        <div id="SubmitFormError" class="tab-pane fade in active">
            <p></p>
            <p id="yui_3_17_2_1_1523943995346_146">Your request has not been successful. You need to
 <a href="/app/contact" class="link-dark-bold" id="yui_3_17_2_1_1523943995346_145">contact Customer Care</a> to arrange a replacement.
            </p>
            <p></p>
        </div>
    </body>
</html>

运行此测试:

public static void main(String[] args) throws Exception {
    final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
    String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
            SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
    if (!new File(pathWebdriver).setExecutable(true)) {
        logger.error("ERROR when change setExecutable on " + pathWebdriver);
    }
    System.setProperty("webdriver.chrome.driver", pathWebdriver);
    final ChromeOptions chromeOptions = new ChromeOptions();
    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.get("file:///C:/Users/xxxxxxx/Desktop/index.html");
    WebElement element = driver.findElement(By.xpath("//div[@id='SubmitFormError']//a"));
    logger.info(element.getText());
    element.click();
    driver.quit();
}

日志控制台:

contact Customer Care

注意:如果您使用Java + Selenium进行Web Interface测试,我建议您使用NoraUi框架

相关问题