简体   繁体   中英

Selenium isSelected() method is lying

I'm trying to test a group of radio buttons with selenium. I've grouped them on a list

@FindBy(xpath = "//ux-button-group//input[@name='options']") 
private List<WebElement> granularity;

When I test that the first option is selected it returns me false .

assertTrue(granularity.get(0).isSelected(), "Granularity should have selected the first option");

I've debugged and saw that if I try with the last element (3), then it gives me true . You may think, as I did, that the order in the list is inverted somehow, but it is not. I've checked, by retrieving the id of each element of the list and they are in the correct order. I also have tried to retrieve the "checked" attribute, and it's wrong. For the first 3 buttons returns null and the for the last one return true !

Html:

<div data-toggle="buttons" class="ux-button-group btn-group  btn-lg" data-e2e="ux-button-group">
    <!----><!---->
    <label class="btn btn-primary btn-lg ng-star-inserted active">
        <input autocomplete="off" name="options" type="radio" id="shared.granularity.0" checked="true"> Daily <!---->
    </label>
    <label class="btn btn-primary btn-lg ng-star-inserted">
        <input autocomplete="off" name="options" type="radio" id="shared.granularity.1" checked="false"> Weekly <!---->
    </label>
    <label class="btn btn-primary btn-lg ng-star-inserted">
        <input autocomplete="off" name="options" type="radio" id="shared.granularity.2" checked="false"> Monthly <!---->
    </label>
    <label class="btn btn-primary btn-lg ng-star-inserted">
        <input autocomplete="off" name="options" type="radio" id="shared.granularity.3" checked="false"> Yearly <!---->
    </label>
    <!----><!---->
</div>

Hmm, xpath doesn't seem correct, the ux-button-group should be a tag to use it like this.

The xpath shoud be:
//div[contains(@class, 'ux-button-group')]//input[@name='options']

or simpler with css:
.ux-button-group input[name=options]

or other css(better since you can see from the id name what represents):
input[id*='shared.granularity']

Also try the selenium 3.141.59 if possible and if you don't have any restrictions.

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