1

I would like to click a chechbox through C# and Selenium. The checkbox HTML is as follows :

<div class="ad-post-rules" ng-class="{'ad-post-rules-error': submitted &amp;&amp; addClassifiedForm.postRulesCheck.$error.required}" an-form-error="" an-form-error-optional-text="İlan verme kurallarını onaylamadınız."><input id="postRulesCheck" class="checkBox sg-checkbox ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" type="checkbox" value="1" name="postRulesCheck" ng-model="postRulesCheck" required="" an-form-object-name="İlan Verme Kuralları"><label for="postRulesCheck"></label><span class="rulesOpen" ng-click="adPostRules=true">İlan verme kurallarını</span><label for="postRulesCheck">okudum, kabul ediyorum</label></div>

My code is as follows :

Dim cekbul As IWebElement
System.Threading.Thread.Sleep(1000)
cekbul = driver.FindElement(By.Id("#postRulesCheck"))
cekbul.Click()
3
  • 1
    Remove the '#' from the id. So say: driver.FindElement(By.Id("postRulesCheck")) Commented Apr 17, 2018 at 9:32
  • element not visible Commented Apr 17, 2018 at 9:37
  • use the javascript executor to click Commented Apr 17, 2018 at 10:28

3 Answers 3

1

I dont know coding in c sharp but i think it works

IWebElement Ele_CheckBox = driver.FindElement(By.Id("postRulesCheck"));

Ele_CheckBox.Click();

By using name

IWebElement Ele_CheckBox = driver.FindElement(By.Name("postRulesCheck"));

Ele_CheckBox.Click();

By xpath

IWebElement Ele_CheckBox = driver.FindElement(By.Xpath("//input[@id='postRulesCheck']"));

Ele_CheckBox.Click();
Sign up to request clarification or add additional context in comments.

1 Comment

ckeck the visiblity using isdisplayed methiod
0

To click on the checkbox as the element is an Angular element you have to induce WebDriverWait for the elelemt to be clickable and you can use either of the following option :

  • CssSelector :

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.checkBox.sg-checkbox.ng-pristine.ng-untouched.ng-empty.ng-invalid.ng-invalid-required#postRulesCheck"))).Click();
    
  • XPath :

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='checkBox sg-checkbox ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required' and @id='postRulesCheck']"))).Click();
    

2 Comments

@HasanSarıkaya Checkout my updated answer as per your updated HTML and let me know the status.
Very thanx dude xpath selection is process.Every body very thanx
0

Selenium can click only on element that is visible for a human. You can still execute javascript even if the element is not visible. If the element is visible for a human while you are trying to execute your test and still you are getting the element not visible exception , try to use Actions API instead otherwise use javascript.

 Actions action = new Actions(driver);
 IWebElement cekbul = driver.FindElement(By.Id("postRulesCheck"));
 action.Click(cekbul).Build().Perform();

This lets you click at a point irrespective of the location of point .

Comments

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.