简体   繁体   English

当硒中的下一个循环不存在数据时,如何打破“ while”条件

[英]How to break the 'while' condition when data is not present for the next loop in selenium

Here my Issue is I have a vendor lookup and i have to be read the vendors for search reasult.Here results is based on search filter (Ex: vendor name begins with,ends with etc.). 这是我的问题,我要进行供应商查找,必须阅读供应商的搜索结果。此处的结果基于搜索过滤器(例如:供应商名称开头,结尾等)。 Here we don't know the count of vendor search results. 在这里,我们不知道供应商搜索结果的数量。 I am reding the vendors with while condition with scrolling to next vendor. 我正在使用滚动到下一个供应商的条件来充实供应商。 Here I am unable to break the the while condition when all the vendors are fetched. 在这里,我无法打破获取所有供应商时的while条件。 So please let me give the answer for How to write the proper condition to while loop 因此,请允许我给出如何向while循环中写入适当条件的答案

try{
    int i=2;
    boolean ele=d.findElement(By.xpath("//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]")).isDisplayed();
    while(ele==true){
        String path="//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]";    // Here i value starts from 2
        String txt=d.findElement(By.xpath(path)).getAttribute("title");
        System.out.println(txt);
        scrollBarHandle("//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]");
        i++;
        ele=d.findElement(By.xpath("//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]")).isDisplayed()
        Thread.sleep(1000);
        }
    }catch(Exception e){
        System.out.println(e.getMessage());
    }

Out put Is: no such element message is displayed After reading all the vednors. 输出为:读取所有吠叫信号后,不显示任何此类元素消息。

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//table[@id='vendorTable']/tbody/tr[9]/td[1]"} org.openqa.selenium.NoSuchElementException:否这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // table [@ id ='vendorTable'] / tbody / tr [9] / td [1]“}

How to break the while loop after all the vendors reading completed. 在所有供应商阅读完后如何打破while循环。

You can first find the no of elements that this selector returns and put an if condition on it : Below is the code : 您首先可以找到此选择器返回的元素编号,并在其上添加if条件:下面是代码:

try{
            int i=2;
            boolean ele=d.findElement(By.xpath("//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]")).isDisplayed();
            while(ele==true){
                String path="//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]";    // Here i value starts from 2
                List<WebElement> elements = driver.findElements(By.xpath(path));
                if (elements.size() == 0) {
                    break;
                }
                String txt=d.findElement(By.xpath(path)).getAttribute("title");
                System.out.println(txt);
                //scrollBarHandle("//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]");
                i++;
                ele=d.findElement(By.xpath("//table[@id='vendorTable']/tbody/tr["+i+"]/td[1]")).isDisplayed()
                        Thread.sleep(1000);
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

Hope it helps you out. 希望它可以帮助您。

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

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