2

I have month dropdown where all the month that appears in the dropdown will be selected by default. The question is, I want only 2050-Jan month checkbox to remain checked and want the other month checkbox to be unchecked. Please note that the Year and Month is Dynamic. i.e. if the dropdown has 3 options displayed then the same dropdown can have 2 options displayed or could have more than 3 options displayed. But the only thing is the Year and month 2050-Jan alone should remain to be unchecked.

So this being the case how do I uncheck the other two or more checkbox.

Automating in Java selenium any leads will be very helpful.

For now if you see the below dropdown has 3 month and Year option selected by default of which I would need only 2050-Jan to remain unchecked.

This is a dynamic dropdown where the Year and Month will change but 2050-Jan will always be there but cant say if this will come as 3rd option always

HTML:

<mat-option _ngcontent-vor-c141="" role="option" class="mat-option mat-focus-indicator mat-option-multiple ng-tns-c92-121 ng-star-inserted mat-selected mat-active" id="mat-option-370" tabindex="0" aria-selected="true" aria-disabled="false" style="">
    <mat-pseudo-checkbox class="mat-pseudo-checkbox mat-option-pseudo-checkbox ng-star-inserted mat-pseudo-checkbox-checked"></mat-pseudo-checkbox>
    <!---->
    <span class="mat-option-text">2022-Apr</span>
    <!---->
    <div mat-ripple="" class="mat-ripple mat-option-ripple"></div>
</mat-option>

Dropdown List HTML:

<div class="mat-select-arrow-wrapper ng-tns-c92-121">

<div class="mat-select-arrow ng-tns-c92-121"></div></div>

HTML after unselecting the checkbox with April 2022:

<mat-option _ngcontent-vor-c141="" role="option" class="mat-option mat-focus-indicator mat-option-multiple ng-tns-c92-121 ng-star-inserted mat-active" id="mat-option-370" tabindex="0" aria-selected="false" aria-disabled="false" style="">
    <mat-pseudo-checkbox class="mat-pseudo-checkbox mat-option-pseudo-checkbox ng-star-inserted"></mat-pseudo-checkbox>
    <!---->
    <span class="mat-option-text">2022-Apr</span>
    <!---->
    <div mat-ripple="" class="mat-ripple mat-option-ripple"></div>
</mat-option>
6
  • Can you provide HTML? Commented Jul 21, 2022 at 10:18
  • The HTML code of drop down list Commented Jul 21, 2022 at 10:24
  • @NandanA - Have added html code for dropdownlist as well Commented Jul 21, 2022 at 10:26
  • @NandanA - Any inputs? Commented Jul 21, 2022 at 10:44
  • Update the question with the additional HTML of the 2022-Apr element when it is unchecked. Commented Jul 21, 2022 at 12:51

1 Answer 1

1

To uncheck the desired you can click() on the element with text as 2022-Apr using the following locator strategy:

new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//mat-option//span[@class='mat-option-text' and text()='2022-Apr']//preceding-sibling::mat-pseudo-checkbox[1]"))).click();

As an alternative you can use removeAttribute method to remove the classname mat-pseudo-checkbox-checked as follows:

WebElement element = driver.findElement(By.xpath("//mat-option//span[@class='mat-option-text' and text()='2022-Apr']//preceding-sibling::mat-pseudo-checkbox[1]"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('class')", element);
WebElement newElement = driver.findElement(By.xpath("//mat-option//span[@class='mat-option-text' and text()='2022-Apr']//preceding-sibling::mat-pseudo-checkbox[1]"));
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('class','mat-pseudo-checkbox mat-option-pseudo-checkbox ng-star-inserted')", newElement);
Sign up to request clarification or add additional context in comments.

7 Comments

Have a Question - After setting the attribute when i try to click on the checkbox it is not happening is there something else that i need to do
Hmmm, I suspected this may happen. Honestly using JavaScript wasn't the ideal approach, should have used the regular click
so what is the alternate ?
Let's discuss the issue in Selenium room.
Thank you for the help made changes to the script where i first go and uncheck the checkbox and then check the required checkbox alone using click method thank you for the help @undetected Selenium
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.