Cannot check a checkbox control using Selenium with C# - c#

I'm trying to check a simple input type=checkbox using Selenium with C#, this is the HTML code I'm using:
<div _ngcontent-c5="" class="check-item">
<input _ngcontent-c5="" class="custom ng-untouched ng-pristine ng-valid" id="captcha" type="checkbox">
<label _ngcontent-c5="" for="captcha" translate="">Text</label>
</div>
I've tried almost all possible approaches like XPath, ID, Class name with no success, this seems to be an Angular label or something like that
Thanks in advance!

Have you tried By.CssSelector("[for='captcha']");? This is a css selector for labels that point to an input with an id.

Related

How to check multiple boxes using C# for Selenium

I am having a problem being able to check both of these "checkboxes" when trying to run a test for Selenium using C#.
Here is what I have for the first checkbox and that does work and checks the box.
driver.FindElement(By.XPath("//label[#for='LegitimateCompanyAgreementCheckBox']")).Click();
However if I simply use the label for the other checkbox, which is "TermsOfServiceCheckBox", it does not check the box, but it actually opens up a link because the this checkbox has 2 hyperlinks inside the wording.
For example if I try using:
driver.FindElement(By.XPath("//label[#for='TermsOfServiceCheckBox']")).Click();
This will not check the box, it will actually open up the link for /terms-of-service.
Here is the code for both checkboxes I need to check.
<div class="asp-checkbox">
<span class="checkbox gaClick" data-category="companySignup" data-action="legitimateCompanyChecked" data-noninteraction="true">
<input id="LegitimateCompanyAgreementCheckBox" type="checkbox" name="ctl00$MainContent$LegitimateCompanyAgreementCheckBox">
<label for="LegitimateCompanyAgreementCheckBox">I am a legitimate estate sale company or auction company and have the documents necessary to conduct business in my state (if any).</label>
</span>
<label for="LegitimateCompanyAgreementCheckBox"></label>
<span id="LegitimateCompanyAgreementCheckBox_requredCheckBoxValidor" controltovalidate="LegitimateCompanyAgreementCheckBox" style="color:Red;visibility:hidden;">*
</span>
</div>
<span class="checkbox gaClick" data-category="companySignup" data-action="agreeToTermsChecked" data-noninteraction="true">
<input id="TermsOfServiceCheckBox" type="checkbox" name="ctl00$MainContent$TermsOfServiceCheckBox">
<label for="TermsOfServiceCheckBox">I agree to EstateSales.NET's Terms of Service
and Privacy Policy.
</label>
</span>
We need not click on label tag in this case. The actual checkbox is the input tag.
Try Xpath as :: driver.FindElement(By.Id("LegitimateCompanyAgreementCheckBox")).Click();
or
driver.FindElement(By.XPath("//input[#id='LegitimateCompanyAgreementCheckBox']")).Click();
Following is with Wait:-
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 120));
WebElement element = wait.until(ExpectedConditions.ElementIsVisible(By.Id("LegitimateCompanyAgreementCheckBox")));
element.Click();

Select radio button with dynamic Id, using CSS selector or Xpath

I'm using C# in Visual Studio to select multiple yes or no questions. Below is the first button to select "yes". The id is not static so can't use that, and I'm not sure which path (CSS or Xpath) is the most efficient for this route.
<div class="row form-group">
<label class="form-label col-md-4 col-lg-3">Was everything satisfactory?</label>
<div class="col-md-7 col-lg-8"><fieldset class="form-group form-radio-group" id="__BVID__385">
<div tabindex="-1" role="group"><div role="radiogroup" tabindex="-1" class="" id="__BVID__386">
<div class="custom-control custom-control-inline custom-radio">
<input type="radio" autocomplete="off" class="custom-control-input" value="true" id="__BVID__387" name="__BVID__386">
<label class="custom-control-label" for="__BVID__387">Yes</label>
</div>
</div>
</div>
Its best to use Xpath when you have dynamic CSS Ids, name.
Based on your requirement ('select multiple yes or no questions') you should have a relation b/w Questions and option.
you can make a Xpath relationship between two elements like in below samples.
//label[contains(.,'Was everything satisfactory?')]/following-sibling::div/fieldset/div/div/div/label[.='Yes' or .='No']
Or
//label[.='Yes' or .='No']/ancestor::div/parent::body/label[contains(.,'Was everything satisfactory')]
Update these Xpaths as per your requirement
If you are better of using XPath or CSS selector comes down to the element at hand and personal preferences. The article linked by E. Wiest has some good points.
In your concrete case, you can either use a CSS selector like :checked
input[type='radio']:checked
if you want to select the subsequent label for instance
input[type='radio']:checked+label
Or some XPath as suggested
//label[contains(text(), "Yes")]/preceding::input
Personally, I also consider XPath to be more powerful but a CSS selector is usually equally adequate.

I cant Locate the Xpath of Pseudo Comment

I just want to know how to get the Xpath Of pseudo Element Actually the thing is that in This I want to .CLick() on this Div Its role Is button so i tried just To xpath with Div Class name it is locating the element but Not clicking i have to Click On this ::after But i cant Get the Xpath Because it is a Pseudo Element
<div id=":l6" class="T-I J-J5-Ji amD T-I-awG T-I-ax7 T-I-Js-Gs L3"
role="button" tabindex="0"
data-tooltip="Older"
aria-label="Older"
style="user-select: none;"
xpath="1">
<span class="amF"
aria-hidden="true">
</span>
<img class="amJ T-I-J3"
src="images/cleardot.gif" alt="">
::after
</div>
What I have read "The Selenium API doesn't support pseudo-elements".
So you can use try using Actions.
I have a sample (I don't have the full path you can add) here with CssSelector:
Actions action = new Actions(Session.Driver);
action.MoveToElement(FindElement(By.CssSelector("div.img::after"))).ContextClick().Build().Perform();
As a last resort, you can use JavaScript with ExecuteScript something like this:
var afterElem = driver.FindElement(By.CssSelector("div.img"));
var buttonAfter = driver.ExecuteScript(
"return window.getComputedStyle(arguments[0],'::after');"
, afterElem);
buttonAfter.click();
You will need to play with the CssSelector Or use an XPath.
Hope this helps you!

Selenium not recognising checkbox with complex attribute

I have the following checkbox on the webapp I am creating selenium scripts for.
The problem I am running into is that my script can't find this checkbox.
My code:
WebDriverWait wait = new WebDriverWait(SELENIUMWebDriver.Driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(
By.XPath("input[#ng-model=\"model['attributes']['3']['attributes']['38'].value\"]"))).Click();
<div class="checkbox" ng-class="{ disabled: form.schema.isDisabled }">
<label>
<input type="checkbox" ng-disabled="form.schema.isDisabled" ng-change="form.onChange(model['attributes']['3']['attributes']['38'].value, form)" ng-model="model['attributes']['3']['attributes']['38'].value" ng-model-options="::form.ngModelOptions" ng-true-value="::'True'" ng-false-value="::'False'" class="ng-pristine ng-untouched ng-valid ng-empty">
<span class="checkbox-label ng-binding" ng-bind="form.title">Registratie EVR/SFH?</span>
</label>
<div class="help-block ng-binding ng-hide" ng-show="::form.description" ng-bind-html="form.description"></div>
</div>
What am I doing wrong?
If you have many checkbox with same className try the following using index
By.XPath("//input[#class='ng-pristine ng-untouched ng-valid ng-empty'][#type='checkbox'][1]");
Updated
By.XPath("//span[contains(text(),'Registratie')]/preceding-sibling::input[#class='ng-pristine ng-untouched ng-valid ng-empty'][#type='checkbox']");
Could you just use this xpath?
//span[contains(text(), 'Registratie EVR/SFH?')]/preceding-sibling::input
I saw that this value can change languages also. If that is the case you could add an "or" into your xpath so either would be acceptable.

Handling dynamically generated elements in selenium webdriver via C#

I am trying to automate testing of a webpage that contains a list of items.
It's a button "Create" in Google Drive with expanding elements.
I have used XPath locator to find button "Create"(driver.FindElement(By.XPath("")), but I don't know how to get access to the expanded elements and click on them by using C# and webdriver.
Here is the code below. Help me, please.
`<div tabindex="0" class="j-Ta-pb f-e f-e-dg a-Da-e" role="button" aria-label="Создать" style="-moz-user-select: none;" guidedhelpid="new_menu_button" aria-expanded="false" aria-haspopup="true">
<div class="j-Ta-pb f-e-og-aa">
<div class="j-Ta-pb f-e-qb-aa">
<div class="j-Ta-pb f-e-rf" aria-hidden="true">Создать</div>
<div class="j-Ta-pb f-e-Tc"> </div></div></div></div>`
You should base on static part. Assuming j-Ta-pb is static/unchanged all the time, you could use this xpath
//div[contains(#class, 'j-Ta-pb')]

Categories

Resources