Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确检查页面标题?_Java_Selenium_Webdriver_Title - Fatal编程技术网

Java 如何正确检查页面标题?

Java 如何正确检查页面标题?,java,selenium,webdriver,title,Java,Selenium,Webdriver,Title,我在Eclipse中使用SeleniumWebDriver 我编写方法来检查标题是否正确显示。代码如下: class Check { String text_to_found; String reason; Check (String t, String r) { text_to_found=t; reason=r; } public void check_title() { try {

我在Eclipse中使用SeleniumWebDriver

我编写方法来检查标题是否正确显示。代码如下:

class Check {
    String text_to_found;
    String reason;

    Check (String t, String r) {
        text_to_found=t;
        reason=r;
    }

    public void check_title() {
        try {
            Assert.assertTrue("Title " + text_to_found + " not found", text_to_found.equals(reason));
        } catch (AssertionError e) {
            System.err.println("title not found: " + e.getMessage());
        }
}
我这样称呼它:

Check title1 = new Check ("Title", driver.getTitle());
title1.check_title();

第一次它工作正常。但第二次(等等),如果我调用这个方法(对于新打开的窗口),它会说找不到标题,但我知道它是正确的。建议,代码有什么问题吗?

我刚刚用你的代码检查了google.com的标题。如果标题不匹配,请在代码中输入-

  Check title1 = new Check ("G3oogle", driver.getTitle());
  title1.check_title();
我们得到结果-
title not found:title G3oogle not found

但如符合以下规定—

Check title1 = new Check ("Google", driver.getTitle());
  title1.check_title();
您没有在控制台上打印任何内容。因此,如果您希望在标题匹配的情况下将任何内容打印到控制台,则可以修改代码-

     public void check_title() {    

     if(text_to_found.equals(reason)){
          System.out.println("title found: Title "+text_to_found+" found");
     }
     else{

         System.out.println("title not found: Title "+text_to_found+" not found");
     }
    }