How to iteratively click all elements in a dropdown with selenium? - selenium

I am testing a react.js front end web application with selenium and C# automation framework, I need to click all the elements in a drop down list, ideally, I would like to select the drop down as a list of elements, and iterate through each element and click it.
I have tried to locate the dropdown menu By Xpath, Cssselector, cssName, none seems to work, when I debug the code, my "dropDown" variable is always null
Here is the code of the drop down menu
<div class="dropdown-menu shadow px-4 show">
<div>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="1">1 </label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="2">2</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox"value="3">3</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="4">4</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="5">5</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="6">6</label>
</div>
</div>
here is my selenium code
public static IList<IWebElement> dropDownClick (IWebDriver _driver) {
IList<IWebElement> dropdown = _driver.FindElements (By.ClassName ("dropdown-menu shadow px-4 show"));
return dropdown
}
I expected the variable "dropdown" is not null when I run the code in debug mode

Please use code written below in order to get the elements and to click on each element in iteration:
//Below line Finds the dropdown
WebElement dropdownElement = driver.findElements(By.xpath("//div[contains(#class,'dropdown-menu')]"));
//Below line stores all elements present in dropdown in a list of webelements
List<WebElement> elements = driver.findElements(By.xpath("//div[contains(#class,'dropdown-menu')]//label"));
for(WebElement ele : elements){
//To click on dropdown
dropdownElement.click();
//To click on label present in dropdown. This will change with each Iteration
ele.click();
}
Hope it helps :)

For xpath why not just use:
//div[#class='dropdown-menu shadow px-4 show']//label ---yields 6 rows
If this is not working, make sure the drop down is not in an iframe. You need the //label added so all elements appear in your "FindElements". Without it your return is 1.

Please try with below xpath. May be it is because of sapce,
//div[contains(#class,'dropdown-menu')]//label

Related

How to select the first option in a radio button group with selenium when its ID changes with every new page load

I am writing a test automation solution with Selenium in Java.
I want to select to first option in a radio button group on my page.
The HTML id and content of these options are dynamically generated.
The options belong to a radio button group with data-cy=tv-select and name=selectTv.
How can I select the first option in this radio button group?
EDIT This is the html code:
<section _ngcontent-hqh-c168="">
<span _ngcontent-hqh-c168="">
<vi-select-tv _ngcontent-hqh-c168="" _nghost-hqh-c167="">
<mat-radio-group _ngcontent-hqh-c167="" role="radiogroup" name="selectTv" data-cy="tv-select"
class="mat-radio-group tvken-select ng-pristine ng-valid ng-star-inserted ng-touched">
<mat-radio-button _ngcontent-hqh-c167="" class="mat-radio-button mat-accent ng-star-inserted" id="mat-radio-8">
<label class="mat-radio-label" for="mat-radio-8-input">
<span class="mat-radio-container">
<span class="mat-radio-outer-circle"></span>
<span class="mat-radio-inner-circle">
</span>
<input type="radio"
class="mat-radio-input cdk-visually-hidden"
id="mat-radio-8-input"
tabindex="0"
name="selectTv"
value="NIEUW_a11fd55d-5792-4f19-8764-ccb188d20591">
<span mat-ripple="" class="mat-ripple mat-radio-ripple mat-focus-indicator">
<span class="mat-ripple-element mat-radio-persistent-ripple">
</span>
</span>
</span>
<span class="mat-radio-label-content">
<span style="display: none;"> </span>tv 1: 02-10-2021 13:20 tot 02-10-2021 15:20</span>
</label>
</mat-radio-button>
<mat-radio-button _ngcontent-hqh-c167="" class="mat-radio-button mat-accent ng-star-inserted mat-radio-checked"
id="mat-radio-9">
<label class="mat-radio-label" for="mat-radio-9-input">
<span class="mat-radio-container">
<span class="mat-radio-outer-circle"></span>
<span class="mat-radio-inner-circle"></span>
<input type="radio"
class="mat-radio-input cdk-visually-hidden"
id="mat-radio-9-input"
tabindex="0"
name="selectTv"
value="new">
<span mat-ripple="" class="mat-ripple mat-radio-ripple mat-focus-indicator">
<span class="mat-ripple-element mat-radio-persistent-ripple"></span>
</span>
</span>
<span class="mat-radio-label-content">
<span style="display: none;"> </span>NEW</span></label>
</mat-radio-button>
</mat-radio-group>
</vi-select-tv>
</span>
</section>
I am interested in the radio button with ID mat-radio-8-input, but that ID changes with every new test run.
This is how I identify my radio button group:
#FindBy(how = CSS, using = "mat-radio-group[data-cy='tv-select']")
private List<WebElement> tv;
How can I select the first option of this group?
You can do this:
#FindBy(how = CSS, using = "mat-radio-group[data-cy='tv-select'] mat-radio-button")
private List<WebElement> radioBtns;
To get first item:
WebElement firstRadioBtn = radioBtns.get(0);
You can do this simply by executing a JS code on the page. Just inspect the target page to find a unique selector for the radio button and set selector.checked = true; using js...
For example, if my radio button have id mat-radio-9, I can use js code :- document.getElementById("mat-radio-9").checked = true;
Check Out These Questions :- Executing js code Check a radio button in js

how to perform click on an element using selenium for which i am getting StaleElementReferenceException

Problem: there are couple of radio button with same id and I need to perform click on these. The first radio button gets the click where the second one is getting StaleElementReferenceException.
Can Anybody Suggest me how it can be handled so that when I click the second radio button it does not throw StaleElementReferenceException
HTML:
<solvup-radio-type _ngcontent-c7="" _nghost-c10=""><div _ngcontent-c10="" class="validation-error ng-untouched ng-pristine ng-invalid">
<solvup-label _ngcontent-c10="" _nghost-c33=""><label _ngcontent-c33="" class="control-label">
Have you been able to resolve the issue based on this information? <!----><span _ngcontent-c33="" class="required-mark">*</span>
</label>
<solvup-tooltip _ngcontent-c33="" _nghost-c36=""><!---->
</solvup-tooltip>
</solvup-label>
<!----><div _ngcontent-c10="" class="radio">
<label _ngcontent-c10="">
<input _ngcontent-c10="" type="radio" id="ts_note_questions" class="ng-untouched ng-pristine ng-invalid">
Yes, issue is resolved - close case
</label>
</div><div _ngcontent-c10="" class="radio">
<label _ngcontent-c10="">
<input _ngcontent-c10="" type="radio" id="ts_note_questions" class="ng-untouched ng-pristine ng-invalid">
No - continue to next step
</label>
</div>
</div>
<solvup-hint-text _ngcontent-c10="" _nghost-c34=""><!----><p _ngcontent-c34="" class="help-block"></p>
</solvup-hint-text>
<solvup-validation-messages _ngcontent-c10="" _nghost-c35=""><!---->
<!----><div _ngcontent-c35="" class="form-group">
<small _ngcontent-c35="" class="err"><i _ngcontent-c35="" aria-hidden="true" class="fa fa-exclamation-triangle"></i> Please select an option</small>
</div>
</solvup-validation-messages>
</solvup-radio-type>
Selenium Code:
#Given("^User fills details in First of Five Troubleshooting page$")
public void user_fills_details_in_First_of_Five_Troubleshooting_page() throws Throwable {
Thread.sleep(2000);
List<WebElement> li = driver.findElements(By.className("radio"));
Actions ob = new Actions(driver);
ob.moveToElement(li.get(1));
ob.click(li.get(1));
Action action = ob.build();
action.perform();
}
Please suggest.
Looking at the dom-structure, you might be trying to click an element that might not be the right one. I would think
List<WebElement> li = driver.findElements(By.Id("ts_note_questions"));
Actions ob = new Actions(driver);
ob.moveToElement(li.get(1));
action.perform();
li.get(1).click();
would work, as you are trying to click on the input instead of the div.
I noticed you copied the first radio-button but it looks like you forgot to change the id of the second radio-button. Try to change the id of the second radio-button to a unique one.

Unable to find element using protactor which have same classes

I have one check box and two links with same classes & same Div.During the automation testing using protractor, i want to click on check box but it click on Links.
i am writing this code but its not working, please provide a solution.
Code:
element(by.id('Remember')).click();
[1
please find HTMl code:-
<div class="input-field">
<div class="pas_rembr">
<input name="remember" id="Remember" class="css-checkbox ng-dirty ng-valid-parse ng-touched ng-not-empty ng-valid ng-valid-required" ng-model="rememberMe" type="checkbox" ng-required="true" required="required">
<!-- <label for="Remember" class="css-label">I agree with the <a class="text_link" target="_blank" ng-href="{{baseUrl}}terms-conditions">Terms & Conditions</a>.</label> -->
<label for="Remember" class="css-label">I have read and I agree with <a class="text_link" target="_blank" ng-href="/lmd/terms-conditions" href="/lmd/terms-conditions">Terms and Conditions</a> and the <a class="text_link" target="_blank" ng-href="/lmd/privacy" href="/lmd/privacy">Privacy Policy</a> of this site.</label>
</div>
<span class="errorForm ng-hide" ng-show="(memberForm.remember.$dirty || submitted) && ((memberForm.remember.$error.required))">
<span class="errorForm ng-scope ng-hide" ng-show="memberForm.remember.$error.required" translate="TERAMS_CONDITION_IS_REQUIRED">Terms and condition is required</span>
</span>
</div>
Try this:-
var el = element(by.css('label[for="Remember"]'));
browser.actions().mouseMove(el, {x: 20, y: 3}).click().perform();
For your tests, the CSS selector would be
element = $$('label.css-label > a:nth-child(2)');
This should be able to click on the second a child of the label element.

Element should have been "select" but was "span" in selenium

Toggle button is present in an web application similar to
http://www.dhtmlgoodies.com/index.html?whichScript=on-off-switch
Search for "option two" in the above link.
Html code of the button is like this when turned Off
<div class="field-block button-height">
<label style="width:175px" class="label" for="input-2"><b>Case Association</b><sup>*</sup>
<span id="reqCaseAssociationTD" style="display: none;">
required</span>
</label>
<p style="margin:0px" class="field switch">
<label style="margin:0 0 4px" class="cb-enable"><span>On</span></label>
<label style="margin:0 0 4px" class="cb-disable selected"><span>Off</span></label>
<input type="checkbox" class="checkbox" id="caseAssociation">
</p>
<span style="margin-top:8px;margin-left:2px" data-tooltip="Simulataneous post from the same user (Twitter and Facebook) would get associated with the previous open case of the user." class="helpText showToolTip"></span>
</div>
Html code of the button is like this when turned ON
<div class="field-block button-height">
<label style="width:175px" class="label" for="input-2"><b>Case Association</b><sup>*</sup>
<span id="reqCaseAssociationTD" style="display: none;">
required</span>
</label>
<p style="margin:0px" class="field switch">
<label style="margin:0 0 4px" class="cb-enable selected"><span>On</span></label>
<label style="margin:0 0 4px" class="cb-disable"><span>Off</span></label>
<input type="checkbox" class="checkbox" id="caseAssociation" checked="checked">
</p>
<span style="margin-top:8px;margin-left:2px" data-tooltip="Simulataneous post from the same user (Twitter and Facebook) would get associated with the previous open case of the user." class="helpText showToolTip"></span>
</div>
Used the following identifiers
//Case Association Turned On
#FindAll(#FindBy(how = How.CSS, using = "a.cb-enable.selected"))
public List<WebElement> caseAssociationIsON;
//Case Association Turned OFF
#FindAll(#FindBy(how = How.CSS, using = "a.cb-disable.selected"))
public List<WebElement> caseAssociationIsOFF;
While running using the selenium getting the error
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element
should have been "select" but was "span"
How do fix this. If more details are needed, please suggest.
I don't see any element that would match your selector, you should use label instead of a, like:
label.cb-enable.selected
and
label.cb-disable.selected
or use a selector based on input. a means link and you don't have any.
In case you still have the error what happens is that you must add an specific route:
In my case:
enter image description here
This is the resultant code:
enter image description here

Handle on-off switch with Selenium / java

I can neither recognize the on-off switch nor get its current value (On / Off).
Please help to have a look
try using xpath = "//span[#class='onoffswitch-inner' and contains(#id,'aui')]" but it doesn't help to recognize this button
"id" is a dynamic one, it will be changed once loading the page
Code:
<li id="aui_3_4_0_1_1099">
<div id="aui_3_4_0_1_1098" class="onoffswitch">
<input id="notificationActive" class="onoffswitch-checkbox" type="checkbox" autocomplete="off" checked="checked" name="notificationActive"></input>
<label id="aui_3_4_0_1_1097" class="onoffswitch-label" for="notificationActive">
<span id="aui_3_4_0_1_1096" class="onoffswitch-inner">
::before
::after
</span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</li>
Thanks,