简体   繁体   English

网页元素比较

[英]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?如何在 Selenium 中处理这些?

I used if-else statement compared both webelement string using getText() method but it is showing NoSuchElementException .我使用 if-else 语句使用getText()方法比较了两个 webelement 字符串,但它显示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 .当然你会得到NoSuchElementException You try to find both WebElements, but you can have present only one at a time.您尝试找到这两个 WebElement,但一次只能找到一个。

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.如果您的操作成功,您将显示此driver.findElement(By.xpath("//div[@text='Deleted successfully']"))和此driver.findElement(By.xpath("//div[@text='Data Cannot be deleted']"))将抛出NoSuchElementException ,反之亦然,因为操作失败。

In your case I recommend you to use try-catch block.在您的情况下,我建议您使用try-catch块。

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 .此外,我会建议您使用Explicit Wait ,让您的应用程序有时间在抛出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 .这将有 10 秒的时间在抛出NoSuchElementException之前查找元素。 You can change this time to how much do you need to increase success of your app.您可以将此时间更改为增加应用程序成功所需的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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