Warm tip: This article is reproduced from serverfault.com, please click

find the label text of required fields in selenium Webdriver

发布于 2020-12-01 19:05:08

I am trying to print "Labels" of all the required fields on Console.

what I have done here is I am putting all the elements in the list and get it printed. But I am still unsure of what logic I should use of filtering only the required fields label.

   List<WebElement> list = driver.findElements(By.xpath("//div[@class='container']"));

         for(int i=0;i<list.size();i++){
             Thread.sleep(2000);
             String name =  list.get(i).getText();
           
            System.out.println(name);

           }

The above code is giving me a count as 0. Maybe I am wrong with the logic or understanding the whole concept.

here is the HTML :

<body>
    <div class="container">
        <div class="logo">
            <img src="https://www.sourcefuse.com/wp-content/uploads/2018/09/Logo-Color.svg" alt="SourceFuse" class="img-responsive" />
        </div>
        <form>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="fname">First Name <span class="required">*</span></label>
                        <div id="fnameInput"></div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="lname">Last Name <span class="required">*</span></label>
                        <div id="lnameInput"></div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="email">Email <span class="required">*</span></label>
                        <div id="emailInput"></div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="curCompany">Current Company <span class="required">*</span></label>
                        <div id="curCompanyInput"></div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="mob">Mobile No. <span class="required">*</span></label>
                        <div id="mobInput"></div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="DOB">Date of Birth <span class="required">*</span></label>
                        <div id="DOBInput"></div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="position">Position you are applying for <span class="required">*</span></label>
                        <div id="positionInput"></div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="portfolio">Portfolio Website <span class="required">*</span></label>
                        <div id="portfolioInput"></div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="salary">Salary requirements <span class="required">*</span></label>
                        <div id="salaryInput"></div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="whenStart">When can you start?</label>
                        <div id="whenStartInput"></div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label for="address">Address</label>
                        <div id="addressInput"></div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="resume">Upload Your Resume <span class="required">*</span></label>
                        <div id="resumeInput"></div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-check">
                        <label for="relocate">Are you willing to relocate? <span class="required">*</span></label>
                        <div id="relocateInput"></div>
                    </div>
                </div>
            </div>
            <button type="submit" class="btn btn-primary">Submit Form</button>
            <button type="reset" class="btn btn-default">Reset Form</button>
        </form>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bootstrap-datepicker.min.js"></script>
    <script src="js/custom.js"></script>
</body>
Questioner
Ghazal shukla
Viewed
0
Norayr Sargsyan 2020-12-02 15:10:38

Your XPATH should be like this for finding the needed label

//span[@class = 'required']//..//..//label

You can write with you way like this:

List<WebElement> list = driver.findElements(By.xpath("//span[@class = 'required']//..//..//label"));

         for(int i=0;i<list.size();i++){
             Thread.sleep(2000);
             String name =  list.get(i).getText();
           
            System.out.println(name);

           }

Or if you are using Java 8 and above like this:

List<WebElement> list = driver.findElements(By.xpath("//span[@class = 'required']//..//..//label"));

list.stream().map(WebElement::getText).forEach(System.out::println);