Check 'checkBox' in another way -> xPath - selenium

I'm using this code:
//input[#type='checkbox'][following-sibling::text()[position()=1]][2]
for checking boxes, but i want to use string, not number (example: 'Uczyń odpowiedzialnym' instead of [2]).
I tried this, but it doesn't work:
//input[#type='checkbox'][following-sibling::text()[position()=1][contains(., 'Uczyń odpowiedzialnym')]]
HTML code:
<input class="prettifiedIeCheckbox ng-pristine ng-valid" type="checkbox" ng-model="holdersModel.OnlyForRead"></input>
<label>
<span class="bottom">
Tylko do wglądu
</span>
</label>
<input class="prettifiedIeCheckbox ng-pristine ng-valid" type="checkbox" ng-model="holdersModel.ChangeMainHolder"></input>
<label>
<span class="bottom">
Uczyń odpowiedzialnym
</span>
</label>
#sideshowbarker help ;)

You need to check the span element's text in the following label sibling node:
//input[#type='checkbox' and following-sibling::label/span = 'Uczyń odpowiedzialnym']
There are, certainly, other ways to locate the element. For instance, check ng-model:
//input[contains(#ng-model, 'ChangeMainHolder')]

You can also use the below xpath:
//span[contains(.,'Uczyn odpowiedzialnym')]/preceding::input[1]
This will select the first input element that is present before the span element with exact innerHTML/text as Uczyń odpowiedzialnym.

I found another (better) solution on sqa.stackexchange by #Sam Woods, modified for my problem:
IWebElement element = driver.findElement(By.xpath("//input[#type='checkbox' and contains(#ng-model, 'ChangeMainHolder')]"));
if (!element.Selected)
{
element.Click();
}

Related

How to iteratively click all elements in a dropdown with 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

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.

Selenium/Python - Element not reachable by keyboard

I am trying to fill in a form automatically. I have recorded a script with Selenium.
One of the field to populate is the zip code. When I start typing the code, a new window opens to suggest appropriate option (javascript autofill)
I need to select the first item the ul (cf. html below)
I am quite new to Selenium and though I have been reading the Selenium/html documentation I am totally stuck for almost 1 month on this...
Many thanks in advance for your support
My code is as follows and I received the error message "Element is not reachable by keyboard"
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
first_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "selected")))
first_option.send_keys(Keys.RETURN)
**HTML**
<div id="localisation_left">
<div class="line toDisable">
<label for="location_p" class="label">Ville ou code postal *</label>
<div class="field-wrapper location-container">
<div class="inputWrapper">
<i id="browserGeoloc" class="icon-geoloc icon-2x blue"></i>
<div class="loaderGif-small hidden"></div>
<input class="nude" name="location_p" id="location_p" autocomplete="off" value="Paris 75010" type="text">
<input name="zipcode" value="" type="hidden">
<input name="city" value="" type="hidden">
<script type="text/javascript">
var numberOfLocation = 1, numberOfAuthorizedLocation = 1;
var cityNewadMultipleLocation = new MultipleLocationNewad('input[name="location_p"]', numberOfLocation, numberOfAuthorizedLocation);
cityNewadMultipleLocation.cityAndZipcodeAreSelected = true;
</script>
<input name="region" value="" type="hidden">
<input name="dpt_code" value="" type="hidden">
</div>
<ul class="location-list visible" style="top: 43px;">
<li data-region="12" data-dpt-code="75" class="selected">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75011</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75015</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75009</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75010</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75017</span>
</li>
You can click on the first option, instead of pressing Enter key
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
condition = EC.visibility_of_element_located((By.CSS,
"label[for='location_p'] + div ul.location-list > li"))
first_option = WebDriverWait(driver, 15).until(condition)
first_option.click()
I had a similar issue, and the above solution did not work for me (it would throw an invalid syntax error).
I first used the find_element_by_css_selector function, which selects the first occurrence of the element with given attributes. This did not work.
Then I used the find_elements_by_css_selector (notice the s), which returns a list of the elements with given attributes. There were two elements in that list. Of course the first one (with index [0]) was not accessible by keyboard: this is equivalent of doing (1) above. But the second element (with index [1]) was accessible by keyboard.
Problem solved.
Try selecting by using Xpath below
elem = driver.find_element_by_id("location_p") elem.send_keys("75")
first_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.Xpath,
".//*[#id='localisation_left']/div/div/ul/li[1]")))
first_option.click()
If anyone faces Element not reachable by keyboard issue, one can also seek below approach:
input_xpath = '//input[#type="file"][#name="files[]"][#class="class_name"]'
input_element = self.driver.find_element_by_xpath(input_xpath)
## to make element visible:
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";',
input_element)
input_element.send_keys('~\Desktop\Release2.pdf')

Unable to locate click the check box

Please help me the locate the check box and select. There are multiple check boxes, there is no way to locate them uniquely.
Here is the Code for one of such check box.
Thanks in Advance for the Help!!!
<div class="col-xs-2 col-sm-2 col-lg-2" style="height:65px">
<ul class="list-inline pull-right">
<li>
<md-input-container class="md-block">
<md-checkbox value="$index+1check" class="checkbox ng-valid ng-dirty ng-touched ng-empty" ng-model="item.Selectedd" ng-click="toggle($index+1, selected,item.TitleId,item)" icon,md-checkbox.md-checked._md-icon="{background-color: green;}" id="Cbk_List" role="checkbox" tabindex="0" aria-checked="false" aria-invalid="false" style=""><div class="_md-container md-ink-ripple" md-ink-ripple="" md-ink-ripple-checkbox=""><div class="_md-icon"></div></div><div ng-transclude="" class="_md-label">
</div></md-checkbox>
</md-input-container>
</li>
<li>
<div class="manageTitle_CirclCard">
<div class="ng-binding">2</div>
</div>
</li>
</ul>
</div>
You should try to locate using cssSelector with it's attribute tabindex as below :-
#Cbk_List[tabindex='0']
Edited :- If checkbox element has not any attributes with unique value, you should try using xpath with index, assuming you want to get first checkbox element, then try below xpath :-
(.//*[#id = 'Cbk_List'])[1]
Note :- In above xpath, just change the index from 1 to desire one checkbox to find.

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,