比较硒中的两个字符串

发布于 2024-11-01 02:42:42 字数 424 浏览 2 评论 0原文

我需要从页面的行中获取值,

我的代码是

String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals(bname1,"HDFC");
if(bname1=="HDFC") {
    System.out.println("Bank name is:"+bname1);
} else {
    System.out.println("Bank name not found");
}
System.out.println(bname1);

结果: 找不到银行名称 HDFC

我的银行名称是“VIJAYA” 但是当我与“bname1”和“VIJAYA”进行比较时,结果会是负数吗? 我如何比较这些字符串 请帮助我...

I need to take value from row from page

my code is

String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals(bname1,"HDFC");
if(bname1=="HDFC") {
    System.out.println("Bank name is:"+bname1);
} else {
    System.out.println("Bank name not found");
}
System.out.println(bname1);

Result:
Bank name not found
HDFC

My bank name is "VIJAYA"
But when i compare to "bname1" and "VIJAYA",RESULT will be negative?
How can i compaire these strings
pls help me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

森林散布 2024-11-08 02:42:42

Java 中不能使用 == 来比较字符串。所做的只是测试两个对象在内存中是否具有相同的地址/是否是相同的实例。使用 .equals() 代替,例如:

if(bname1.equals("HDFC"))

...或者最好是:

if("HDFC".equals (bname1))

哪个更好,因为如果 'bname1' 为 null,它不会崩溃。

You cannot compare strings in Java using ==. All that does is test to see if the two objects have the same address in memory/are the same instance. Use .equals() instead, like:

if(bname1.equals("HDFC"))

...or preferably:

if("HDFC".equals(bname1))

Which is better because it won't crash if 'bname1' is null.

呆橘 2024-11-08 02:42:42

您的原始代码还有其他一些问题。当您调用assertEquals()时,它告诉编译器检查两个值是否相等,如果条件不成立则停止执行代码。因此,如果断言失败,则在assertEquals()语句之后找到的打印语句将不会被执行。相反,您应该尝试执行以下操作。

String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals("Bank name - "+bname1+" should be HDFC",bname1,"HDFC");

如果您这样编写语句,则assertEquals函数的第一个参数将成为断言失败时显示的错误消息。

Your original code has some other issues as well. When you call assertEquals() that tells the compiler to check if the two values are equal and then stop executing code if the condition is not true. Because of this the print statements found after your assertEquals() statement will not get executed if the assertion fails. Instead you should try to do something like the following.

String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals("Bank name - "+bname1+" should be HDFC",bname1,"HDFC");

If you write your statement like this the first parameter of the assertEquals function will become the error message displayed when the assertion fails.

菊凝晚露 2024-11-08 02:42:42

试试这个..

        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("Url");

        WebElement strvalue = driver.findElement(By.xpath("  "));
        String expected = "Text to compare";
        String actual = strvalue.getText();
        System.out.println(actual);

    if(expected.equals(actual)){
        System.out.println("Pass");
    }
        else {
            System.out.println("Fail");
        }

Try this..

        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("Url");

        WebElement strvalue = driver.findElement(By.xpath("  "));
        String expected = "Text to compare";
        String actual = strvalue.getText();
        System.out.println(actual);

    if(expected.equals(actual)){
        System.out.println("Pass");
    }
        else {
            System.out.println("Fail");
        }
一腔孤↑勇 2024-11-08 02:42:42

我相信它会对你有帮助

assertEquals(bname1,"HDFC");
if(bname1.equals("HDFC")) {
    System.out.println("Bank name is:"+bname1);
} else {
    System.out.println("Bank name not found");
}
System.out.println(bname1);```

I'm sure It will help you

assertEquals(bname1,"HDFC");
if(bname1.equals("HDFC")) {
    System.out.println("Bank name is:"+bname1);
} else {
    System.out.println("Bank name not found");
}
System.out.println(bname1);```
祁梦 2024-11-08 02:42:42

您可以简单地使用如下断言:

Assert.assertEquals(ExpectedString, ActualString);

这里 ExpectedString 是预期的字符串,ActualString 是原始字符串值。

You can simply use assertions like:

Assert.assertEquals(ExpectedString, ActualString);

Here ExpectedString is a string that is expected and ActualString is original String value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文