java—在SeleniumWebDriver中查找所需字段的标签文本

bwntbbo3  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(296)

我正在尝试打印控制台上所有必需字段的“标签”。
我在这里所做的是我把所有的元素在列表中,并得到打印出来。但是我仍然不确定我应该使用什么逻辑来只过滤必填字段标签。

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);

           }

上面的代码给了我一个0的计数。也许我的逻辑错了,或者对整个概念的理解错了。
以下是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>
sgtfey8w

sgtfey8w1#

为了找到所需的标签,xpath应该是这样的

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

你可以这样写:

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);

           }

或者,如果您使用的是java 8及更高版本:

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

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

相关问题