0

I have this code below which seems to not go to the else if statement. It runs the first part but never seems to go to part 2, if element 1 is already selected.

Thanks

if (!driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[1]/div/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/span/span[1]")).isSelected() ) {
    driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[1]/div/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/span/span[1]")).click();
} else if(!driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[1]/div/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/span/span[2]")).isSelected() ) {
    driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[1]/div/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/span/span[2]")).click();
}

What I want to happen is:

If Element 1 is not selected

Click it

Else Click Element 2

2
  • So if the first if statement evaluates to a true condition you will never get to the code in the else if. If you want that second if to always be evaluated it needs to be a stand alone if and not an else if. If you can clarify what you are trying to accomplish we can give you more precise help.
    – Dan Snell
    Commented Mar 24, 2015 at 22:38
  • Yes, If the first if is true ignore the else if, but it seems like else if is never run and just skipped even if the first statement is false.
    – Elsid
    Commented Mar 25, 2015 at 13:38

1 Answer 1

1

I made your code readable. Please do that from now on.

Your flow is as follows...

  • if span[1] is not selected then
  • click span[1]
  • else if span[2] is not selected then
  • click span[2]

Please clarify what precisely is happening so we can help you properly.

EDIT

As I mention above your second part is checking if span[2] is not selected...

  • If Element 1 is not selected
  • Click it
  • Else Click Element 2

Here is how you can follow your logic

if (!driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[1]/div/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/span/span[1]")).isSelected() ) {
    driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[1]/div/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/span/span[1]")).click();
} else {
    driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[4]/div[1]/div/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/span/span[2]")).click();
}
3
  • That is exactly what is happening. If 1 is not selected click 1, else if 2 is not selected click 2. Since only 1 can be selected at a time, two will never be selected if it goes to the else statement.
    – Elsid
    Commented Mar 25, 2015 at 13:14
  • so what do you want to happen? you have not made that clear in the question Commented Mar 25, 2015 at 13:16
  • edited question see above
    – Elsid
    Commented Mar 25, 2015 at 13:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.