简体   繁体   中英

why an else block doesn't get executed even though the condition is false in Java in Selenium

i have these below lines of code:

 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`
   }

The above piece of code runs fine as long as y has not null values. But when y is blank(ie null) I expect the else block to be executed. But it doesn't happen so, control still goes to if. when i print y value to the console before the condition it shows blank. What am i missing here?

Seems like you need an empty string check as well:

Instead of:

if (y !=null)

try this:

if (y !=null && !y.isEmpty()) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM