简体   繁体   中英

WebElement Comparison

In my project, I have several datas with checkboxes if I click those different set of data and try to delete that I am getting two types of alerts: one is "deleted successfully" for one data and for other data it showing "data cannot be deleted" popup. How to handle these both in Selenium?

I used if-else statement compared both webelement string using getText() method but it is showing NoSuchElementException .

Here is my code:

WebElement Popup = driver.findElement(By.Xpath="//input[@class='btn-btn-popup']")

WebElement e = driver.findElement(By.xpath="//div[@text='Deleted successfully']");
String Deletepopup = e.getText();

WebElement f = driver.findElement(By.xpath="//div[@text='Data Cannot be deleted']");
String CannotDeltedPopup = f.getText();

if (Deletepopup.equals("Deleted Successfully")) {
    Popup.click();
}
else if (CannotDeletedPopup.equals("Data Cannot be deleted")) {
    Popup.click();
}

Of course you get NoSuchElementException . You try to find both WebElements, but you can have present only one at a time.

If your action succeed you will have present this driver.findElement(By.xpath("//div[@text='Deleted successfully']")) and this driver.findElement(By.xpath("//div[@text='Data Cannot be deleted']")) will throw NoSuchElementException and vice versa for action failed.

In your case I recommend you to use try-catch block.

String txt;
try{

    txt = driver.findElement(By.xpath("//div[@text='Deleted successfully']")).getText();

}catch(NoSuchElementException e){

    try{

        txt = driver.findElement(By.xpath("//div[@text='Data Cannot be deleted']")).getText();

    }catch(NoSuchElementException e1){

        txt = "None of messages was found"; //this will happend when none of elements are present.

    }

}

I this case you will try to find message 'Deleted successfully' and if it is not present will try to find message 'Data Cannot be deleted'.

Also I will recommend you to use Explicit Wait , to give your app some time to look for you element before to throw NoSuchElementException .

String txt;
try{

    WebDriverWait wait=new WebDriverWait(driver, 10);

    txt = wait.until(ExpectedConditions.visibilityOfElementLocated(
                        By.xpath("//div[@text='Deleted successfully']"))
                    ).getText();

}catch(NoSuchElementException e){

    try{

        WebDriverWait wait=new WebDriverWait(driver, 10);

        txt = wait.until(ExpectedConditions.visibilityOfElementLocated(
                            By.xpath("//div[@text='Data Cannot be deleted']"))
                        ).getText();

    }catch(NoSuchElementException e1){

        txt = "None of messages was found"; //this will happend when none of elements are present.


    }

}

This will give 10 seconds time to look for element before to throw NoSuchElementException . You can change this time to how much do you need to increase success of your app.

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