簡體   English   中英

為什么即使在 Selenium 中的 Java 中的條件為假時 else 塊也不會被執行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM