虽然当我执行pageSource()。contains。(text)时,文本出现在pagesource()中,但它返回false

时间:2017-04-28 07:48:45

标签: java selenium selenium-webdriver selenium-chromedriver

我正在尝试找到副标题文字:"如果您有farzanshaikh.com电子邮件地址,则可以在未经批准的情况下加入此团队。" 尽管此文本存在于页面源中,但它始终为.contains()

返回false

代码:

package JUnitTesting;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestOne {
    WebDriver driver;
    String BaseUrl;
    String TeamDomain = "farzanshaikh.com";

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        BaseUrl = "http://farzanshaikh.flock.co/";
        driver.get(BaseUrl);
        driver.manage().window().maximize();
    }

    @Test
    public void test() {
        String element = driver.getPageSource();
        System.out.println(element);

        if(driver.getPageSource().contains("You can join this team without approval if you have a "+TeamDomain+" email address.")){
            System.out.println("The Sub Heading on the Team URL page is Correct");
        }
        else{
            System.err.println("The Sub Heading on the Team URL page is Wrong.");
        }
    }

    @After
    public void tearDown() throws Exception {
        Thread.sleep(2000);
        driver.quit();
    }


}

1 个答案:

答案 0 :(得分:1)

好吧,我在你的代码或结果中都没有看到任何问题。

当我们查看网页时,我们可以看到文字You can join this team without approval if you have a farzanshaikh.com email address.

但是当我们尝试获取pagesource时,文本中包含文本的部分就像:You can join this team without approval if you have a <span class='domains-list'>{{canJoinDomains}}</span> email address.

最后,您尝试使用contains来验证第二个字符串中是否存在第一个字符串。因此它失败了。如果您可以将第一个字符串缩减为You can join this team without approval,则返回True。

如果这回答了你的问题,请告诉我。