繁体   English   中英

C#硒如何单击复选框

[英]c# selenium How to click on a checkbox

我想通过C#和Selenium单击一个复选框。 复选框HTML如下:

<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>

我的代码如下:

Dim cekbul As IWebElement
System.Threading.Thread.Sleep(1000)
cekbul = driver.FindElement(By.Id("#postRulesCheck"))
cekbul.Click()

要单击复选框,因为该元素是Angular元素,则必须诱使WebDriverWait使elelemt可单击,并且可以使用以下任一选项:

  • 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(); 

硒只能单击人类可见的元素。 即使该元素不可见,您仍然可以执行javascript。 如果在尝试执行测试时该元素对于人类是可见的,而您仍在获取该元素不可见的异常,请尝试使用Actions API,否则请使用javascript。

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

这样,无论点的位置如何,都可以单击某个点。

我不知道用C Sharp编码,但我认为它有效

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

Ele_CheckBox.Click();

通过使用名称

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

Ele_CheckBox.Click();

通过xpath

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

Ele_CheckBox.Click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM