0

I have a web page that have an indicator if a specific stock is opened/closed. For that I want to set a if & else if statement. For some reason I get an error in eclipse.

*Note: Don't know if it helps, but if I copy/paste the code to a new empty class that is a 'Main' one, it works without any errors - so the structure of the code is Ok I guess.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class TestClock {

public static void chromeMethod(WebDriver driver) {
    System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
}

public static void runTestClock(WebDriver driver)
{


    WebElement Status = null;
    if( Status == driver.findElement(By.cssSelector(".redClockBigIcon")));
    {
        System.out.println("Stock is closed");
        driver.navigate().back();
    }

    else if (Status == driver.findElement(By.cssSelector(".greenClockBigIcon")) );
    {
        System.out.println("Stock is open");

        driver.navigate().back();
    }

}
}

I get a syntax error under 'else' :

Syntax error on token "else", delete this token

1
  • 4
    You have a stray extra ;. Commented Dec 5, 2017 at 13:15

3 Answers 3

2
else if (Status == driver.findElement(By.cssSelector(".greenClockBigIcon")) );

Here is your mistake: the last ; has to be deletet

Sign up to request clarification or add additional context in comments.

Comments

0

The code you posted has a few issues.

  1. You put ; at the end of the if and else if statements. They should both be removed.

  2. You are comparing an element Status (that is currently null) to a .findElement() return. If the element is not found, it will throw an exception, not return a null element. The way to get around this is to use .findElements() (note the plural, Elements) and check to see if the list is empty. If it is empty, the element was not found.

  3. Your .back() statement exists in both cases. Because of this, it can be moved out of the if and will get executed either way.

Rewritten code

public static void runTestClock(WebDriver driver)
{
    if (driver.findElements(By.cssSelector(".redClockBigIcon")).isEmpty())
    {
        System.out.println("Stock is closed");
    }
    else if (driver.findElements(By.cssSelector(".greenClockBigIcon")).isEmpty())
    {
        System.out.println("Stock is open");
    }
    driver.navigate().back();
}

Having said all that... by looking at the CSS selectors you are using, I would guess that green means the stock is open and red means it's closed. This would mean that you have your logic backwards. I think you will need to switch your messages open <-> closed but you would know better than I...

5 Comments

Your answer is amazing, I did switch the values of the CSS as you recommended at the last note. I have questions for learning please: 1) Why do we use Elements and not element ? 2) Why should we use .isEmpty(), can't we use just if & elseIf & else for exception? Is this the same? Thanks a lot!!!
1) So .findElement() is intended to return a single element. The problem is that if that intended element does not exist, it throws an exception. Using .findElements() always returns a collection. If a single element was found, the list will have a single element. If no elements were found, the list will be empty... and so on. Even if the list is empty, no exception is thrown so it's the preferred way to determine if a locator returns elements from the page.
2) We need to use .isEmpty() because that's testing to see if the returned list is empty. If the list is empty, that means the element doesn't exist on the page. It's the condition that the ifs are using to determine whether the stock is open or closed.
Thanks a lot for explaining !
If you found this, or any other answer, useful please upvote it. When you find the answer to your question, please mark it as accepted so your question doesn't remain unanswered.
0

Please remove the ; (semi-colon) at the end of if and else if statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.