0

I'm writing an automation test for a web page, where User has to click on a button based on its presence

I'm using Selenium WebDriver with Java Scenario 1. Only one button can be present at a time (button1 or button2) 2. If button1 is present, User clicks on that 3. If button2 is present, User clicks on that

Code:

 if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).isDisplayed() ) {
        //clicking on button1 if its presemt

        WebElement clickBtn1 = driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div"));
        clickBtn1.click();

 }
    else if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).isDisplayed() ){
    //Clicking on button2 if its present

    WebElement clickBtn2 = driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div"));
    clickBtn2.click();
}

else
    {
        System.out.println("No such button found");
    }

The above code snippet doesn't work. Can someone, please help to correct it.

Note: If I remove the if-else conditions and run separately for each button functions it works

Thanks,

Maor Bachar
  • 81
  • 1
  • 8
Harshini
  • 353
  • 2
  • 6
  • 12
  • What does not work? Does it it say "No such button found"? – nCessity Jun 16 '17 at 10:35
  • yes. It throws the error "org.openqa.selenium.NoSuchElementException: no such element:" If I run the code separately for two buttons without if else condition it works – Harshini Jun 16 '17 at 10:41
  • Those xpath expressions are very long. Can you verify that they do work? Can you simplify them by adding an id to the buttons? – nCessity Jun 16 '17 at 10:44
  • well, if I remove the if-else condition and run the codes for button clicks separately, they work. – Harshini Jun 16 '17 at 10:46
  • 1
    And are both buttons present in that document, even if they are not displayed? I am asking, because that exceptions seems to say that one of them is not even in the document. But maybe you did already think of that... – nCessity Jun 16 '17 at 10:53
  • 1
    In that case: Check if they are present, (https://stackoverflow.com/questions/7991522/selenium-webdriver-test-if-element-is-present) and then check if they are displayed: – nCessity Jun 16 '17 at 11:01
  • hey, thanks. I did think of the presence of button elements. But didn't realize that I need to first check that the button is not visible.. your suggestion helped.. I tried "isEmpty" option which worked – Harshini Jun 19 '17 at 08:54

2 Answers2

0

Before your if-else logic try to use explicit wait.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")));

Let me know if this helps.

Alok
  • 1,441
  • 4
  • 20
  • 31
0

Thanks everyone who helped. I'm posting here the working piece of code if anyone will need this in future.

Scenario: - The web page has two buttons - button1 and button2. - button1 will be initially present - If user clicks on button1 initially, second time button2 wil be present. (At this instance, button1 is hidden from UI)

Code Block:

if(!driver.findElements(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).isEmpty()){

//Clicking on button2, which appears when button1 is not present
                WebDriverWait wait = new WebDriverWait(driver, 30);
                wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")));

                Thread.sleep(5000);
                driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).click();

        }

        else if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).isEnabled() ){

//clicking on button1
                WebDriverWait wait = new WebDriverWait(driver, 30);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")));

        driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).click();


    }
    else
        {
            System.out.println("No such button found");
        }
        Thread.sleep(5000);
Harshini
  • 353
  • 2
  • 6
  • 12