![](/img/trans.png)
[英]Return statement doesn't break out of block, even though condition is true
[英]why an else block doesn't get executed even though the condition is false in Java in Selenium
我有以下代碼行:
WebElement x = driver.findElement(By.xpath<Locator>);
y = x.getText();
sysout(y);
if (y !=null)
{ <few statements here>}
else
{ sysout(" The value is blank in the UI");
<few other statements>`enter code here`
}
只要 y 沒有 null 值,上面的代碼就可以正常運行。 但是當 y 為空白(即 null)時,我希望執行 else 塊。 但事實並非如此,控制權仍然存在於 if 中。 當我在條件顯示為空白之前將 y 值打印到控制台時。 我在這里想念什么?
似乎您還需要一個空字符串檢查:
代替:
if (y !=null)
嘗試這個:
if (y !=null && !y.isEmpty())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.